Reputation: 431
i am developing a module in php where i am supposed to manage time differences and proceed at a specific time difference. i am fetching a value from database ($end_date) which contains time.
$end_date = $result['end_date'];
getting the server time from
$date = $this->GetServerTime();
getting the time difference between these two
$diffrence = date("Y-m-d H:i:s",strtotime("$date - $end_date"));
now, i need to check IF the time difference is less than 15 seconds via if statement. my approach:
$to_time = strtotime("0000-00-00 00:00:15");
if ($difference < $to_time)
{
echo "here";
}
but i believe i am not doing something right, how else should i compare this? i've done a lot of research but to no avail, any help is appreciated.
Upvotes: 1
Views: 2808
Reputation: 1828
One way to do this could be using something like this
$to_time = strtotime("2008-12-13 10:42:00");
$from_time = strtotime("2008-12-13 10:21:00");
$difference = $to_time - $from_time;
if($difference > 15){
<CODE>
}
And for those who use a different date / time format than above, can use something like the below code, you can use the date() function's parameters to specify your date format.
$dateFormat = "d/m/Y H:i:s";
$tmp_to_time = date_create_from_format($dateFormat,"25/08/2015 10:45:12");
$tmp_from_time = date_create_from_format($dateFormat,"25/08/2015 10:34:15");
$to_time = strtotime(date_format($tmp_to_time , $dateFormat))
$from_time = strtotime(date_format($tmp_from_time , $dateFormat))
$difference = $to_time - $from_time;
if($difference > 15){
<CODE>
}
Inspiration and main part of code found from here
Upvotes: 3
Reputation: 431
This is how it could be done :
$end_date = strtotime($result['end_date']);
$date = strtotime($this->GetServerTime());
$difference = $date-$end_date;
$sec = strtotime(15);
if ($difference < $sec){
echo "here";
}
Upvotes: 0
Reputation: 2239
Whenever I have to do something similar, I just calculate with timestamps (which are in seconds).
$end_date = strtotime($result['end_date']); # timestamp in s
$date = strtotime($this->GetServerTime()); # timestamp in s
if ($date - $enddate > 15) {
echo "Here";
}
For minutes and hours you can multiply these times easily with 60.
if ($date - $enddate > (15*60)) { # 15 minutes
echo "Here";
}
I am not entirely sure this will work with daylight saving time, but it should suffice for most use cases.
Oh, btw: This is really helpful if you have to compare with the current time, as time()
returns a timestamp in seconds.
Upvotes: 1