Date Comparison in PHP and MYSQL

I'm trying to figure out what is the best way

  • Capture current date / time ( date('Y-m-d H:i:s')better?)
  • Get db date from SQL
  • Make sure that the current date (No. 1) is between the date displayed from the database and 5 hours after it.

I am new to php.

+3
source share
2 answers

Pull the date you want from MySQL

  SELECT myDateCol FROM myTable WHERE ...

Convert this to a UNIX timestamp

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

Check if the date from the database for the last 5 hours is indicated.

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

Use PHP DateTime class:

$start = new DateTime("2009/09/17 18:52:31");
//Your SQL request should set this

$end = date_add($start,new DateInterval("PT5H"))

$now = new DateTime();

if($start<$now && $now<$end)
{
//etc
}
0
source

Source: https://habr.com/ru/post/1717954/


All Articles