PHP check if timestamp is greater than 24 hours from now
Asked Answered
W

3

15

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?

Wonderland answered 13/7, 2013 at 4:45 Comment(0)
H
19
$date = strtotime("2013-07-13") + strtotime("05:30:00");

should be

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

See difference in this CodePad

Hymn answered 13/7, 2013 at 4:48 Comment(3)
I can see that, the problem is that the date and the time are how the values are stored in the db. They are stored as a date and a time, I cannot change it.Wonderland
@Wonderland Don't change them, concatenate them.Hymn
because actually strtotime("05:30:00"); equals to strtotime("today 05:30:00");.Unbearable
C
9
<?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
}
?>
Cyndi answered 7/10, 2014 at 12:9 Comment(0)
G
6

Store the values of date and time in separate variables and convert it into a Unix timestamp using strtotime() after concatenating the variables.

Code:

<?php

$date = "2013-07-13";
$time = "05:30:00";

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

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

?>
Gambrinus answered 13/7, 2013 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.