L
L
lexstile2021-06-16 15:46:15
MySQL
lexstile, 2021-06-16 15:46:15

How to correctly compare different date formats?

The date is stored in this format:
60c9f19f13534080529687.png
SQL query:

$period = 0;
        $time = time();
        
        switch ($data->filter) {
          case 'week': $period = $time - (60 * 60 * 24 * 7); break;
          case 'month': $period = $time - (60 * 60 * 24 * 30); break;
          default: $period = $time - (60 * 60 * 24); break;
        }
        
        $data = (object)[
          'user_id' => $data->userId,
          'period' => $period,
        ];
        
        $enrollments = $this->model->getEnrollments($data);


  public function getEnrollments($data) {
    $params = [
      'user_id' => $data->user_id,
      'period' => $data->period,
    ];

    return $this->db->row('SELECT id, amount, DATE_FORMAT(date, "%h:%i:%s %d.%m.%Y") as date FROM enrollments WHERE user_id = :user_id && status = 1 && FROM_UNIXTIME(date) > :period ORDER BY id DESC', $params);
  }


How to compare :period with date in db? ( :period - number, number of seconds since the beginning of 1970)
As you can see, I try FROM_UNIXTIME, but it doesn't work.
How to compare?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Adamos, 2021-06-16
@lexstile

number of seconds since the beginning of 1970

It's called UNIX_TIMESTAMP. It may be necessary to take into account the correction for the time zone.

R
Rsa97, 2021-06-16
@Rsa97

If you need to compare with the current time, then`date` > NOW()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question