Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP check if timestamp is greater than 24 hours from now

I have software that needs to determine if the cutoff datetime is greater than 24 hours from now. Here is the code I have to test that.

 $date = strtotime("2013-07-13") + strtotime("05:30:00");

 if($date > time() + 86400) {
    echo 'yes';
 } else {
    echo 'no';
 }

My current date and time is 2013-07-13 2am. As you can see its only 3 hours away. At my math thats 10800 seconds away. The function I have is returning yes. To me this is saying the $date is greater than now plus 86400 seconds when in fact its only 10800 seconds away. Should this not be returning no?

like image 870
Naterade Avatar asked Jul 13 '13 04:07

Naterade


People also ask

How can I check if a date is greater than today in PHP?

php $date_now = time(); //current timestamp $date_convert = strtotime('2022-08-01'); if ($date_now > $date_convert) { echo 'greater than'; } else { echo 'Less than'; } ?> Save this answer.

Can you compare time in PHP?

In order to compare those two dates we use the method diff() of the first DateTime object with the second DateTime object as argument. The diff() method will return a new object of type DateInterval .

What is timestamp format in PHP?

What is a TimeStamp? A timestamp in PHP is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT).


1 Answers

<?php
date_default_timezone_set('Asia/Kolkata');

$date = "2014-10-06";
$time = "17:37:00";

$timestamp = strtotime($date . ' ' . $time); //1373673600

// getting current date 
$cDate = strtotime(date('Y-m-d H:i:s'));

// Getting the value of old date + 24 hours
$oldDate = $timestamp + 86400; // 86400 seconds in 24 hrs

if($oldDate > $cDate)
{
  echo 'yes';
}
else
{
  echo 'no'; //outputs no
}
?>
like image 200
Vijay Avatar answered Sep 17 '22 12:09

Vijay