Home » Php » Checking if mySql datetime is older then 1 day from php now()

Checking if mySql datetime is older then 1 day from php now()

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have a record returned from MySQL that has a datetime field. What I want to do is take this value and see if it is older then 24 hours, I presume using PHP’s time() to get the current time.

At the moment if I echo them out I get:

 1276954824            this is php's time()
 2010-06-19 09:39:23   this is the MySQL datetime

I presume the top one is a unix time? Have been playing around with strtotime but with not much success..

ANy help welcome!

How to&Answers:

No success?

echo strtotime("2010-06-19 09:39:23");

gives me

1276940363

(mktime(9, 39, 23, 6, 19, 2010) gives the same time, so the parsing works correctly)


To get the differences in seconds, you can substract the timestamps, e.g.

$diff = time() - strtotime("2010-06-19 09:39:23");

If the differences is larger than 86400 (60*60*24) seconds, then the timestamps are more than one day apart:

if(time() - strtotime("2010-06-19 09:39:23") > 60*60*24) {
   // timestamp is older than one day
}

Answer:

You can also do:

SELECT * FROM my_table WHERE timestamp < NOW() - INTERVAL 1 DAY;

Answer:

Why are you mixing PHP times and MySQL times?

Instead, do the comparison directly in MySQL:

To get the current date/time in MySQL use the NOW() function. You can compare, for example, 2010-06-19 09:39:23' < DATE_SUB(NOW(), INTERVAL 1 DAY)

This would check to see if the given date (presumably in a column) is older than 24 hours.

If it’s absolutely necessary to convert a MySQL timestamp to a UNIX timestamp, you can use MySQL’s UNIX_TIMESTAMP() function to do so.

Answer:

I wrote a function, by which you can determine if the first given date is one day or n days bigger or smaller than the second given date.

$date1 = "2013/03/01";
$date2 = "2013/03/01";
$sign  = "-";
$diff  = 1;
$result = isDaysSmallerBigger($date1, $date2, $sign, $diff);
var_dump($result);

/**
 * Note: this function is only supported in php 5.3 or above
 * 1. If $date1 - $date2 = $sign $diff days, return true;
 * 2. If $date1 equals $date2 and $diff euqals 0, whether $sign is "+" or "-", return true
 * 3. Else return false; 
 * @param unknown_type $date1
 * @param unknown_type $date2
 * @param string $sign: "-": $date1 < $date2; "+": $date1 > $date2; 
 * Besides if $diff is 0, then both "-" and "+" means $date1 === $date2;
 * @param integer $diff: date difference between two given dates
 */
function isDaysSmallerBigger($date1, $date2, $sign, $diff) {
    $dateTime1 = new DateTime($date1);
    $dateTime2 = new DateTime($date2);

    $interval = $dateTime2->diff($dateTime1);
    $dayDiff  = $interval->format('%a');
    $diffSign = $interval->format('%R'); 

    if((int)$dayDiff === (int)$diff) {
        // Correct date difference

        if((int)$diff !== 0) {
          // Day difference is not 0
          if($diffSign === $sign) {
            return true;
          } else {
            return false;
          }
        } else if((int)$diff === 0) {
          // Day difference is 0, then both given "+" and "-" return true
          return true;
        } 
    } else {
        // Incorrect date difference
        return false;   
    }   
}