Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing dates in PHP and MYSQL

Tags:

sql

php

datetime

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.

like image 917
An employee Avatar asked May 16 '26 13:05

An employee


1 Answers

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';
like image 126
pix0r Avatar answered May 18 '26 10:05

pix0r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!