Compare current date/time to database record

I have set my time zone: date_default_timezone_set(“America/Chicago”);

I have an endDateTime database stored timestamp in yyyy-mm-dd hh:mm:ss format.
When endDateTime is in future the below statement evaluates as 1 (true).
(strtotime($row[‘endDateTime’]) < strtotime(now))
I thought this would evaluate as false.

Should I use a different function than strtotime?

The $row variable probably doesn’t contain what you expect and my not exist at all. When a comparison doesn’t work as expected, you need to examine the values being compared to find out why (you cannot assume anything in programming.)

Use var_dump() on the $row variable, right before the comparison statement, to check what it contains.

Next. the use of now is incorrect, but currently works. strtotime() expects a string. It should be ‘now’. You would be getting a php error from this. Do you have php’s error_reporting set to E_ALL and display_errors set to ON, so that php would help you by reporting and displaying all the errors it detects? This would also help you find if the $row variable doesn’t exist or the associate index name doesn’t exist.

Lastly, the standard date/time format is directly comparable and you should be able to do any date/time comparisons in an sql query. What overall goal are you trying to accomplish by performing a comparison in php code that cannot be accomplished in the sql query?

Sponsor our Newsletter | Privacy Policy | Terms of Service