Php – comparing dates in PHP and MYSQL

datetimePHPsql

I am trying to figure out what is the best way to

  1. Grab the current date/time (is date('Y-m-d H:i:s') best?)
  2. Grab the db date from SQL
  3. Check if the current date (#1) is between the date pulled from the DB and 5hours after it

I'm a newbie at php.

Best Answer

Pull the date you wanted from MySQL

  SELECT myDateCol FROM myTable WHERE ...

Convert this into a UNIX timestamp

$db_timestamp = strtotime($db_row['myDateCol']);

Check if the date from the DB is within the last 5 hours

if ($db_timestamp >= strtotime('-5 hours')) echo 'DB date is within last 5 hours';