Time is confusing... How can one calculate the time elapsed from PM to AM in php?
For example 22:00:00 (10pm) to 02:00:00 (02am) should give 04 hours elapsed. Instead my code returns -08 hours
function hours_elapsed()
{
$timestart = strtotime("22:00:00");
$timestop = strtotime("02:00:00");
$time_diff = $timestop - $timestart; //time difference
return gmdate("h", $total_hours);
}
It's clear that the code calculates in 24 hour format so of course it returns -08 but how is it possible to get time elapsed without 24 hour constraint.. when it passes the midnight mark?
-8 hours is 4 hours. Just add 12 if the number is negative:
$time_diff = $timestop - $timestart; //time difference
if ($time_diff < 0) {
$time_diff += 12;
}
This won't help you, however, if your dates are more than one day apart. You need to specify the date as well (as in the day of the month) to tell PHP that those times are different.
Clearly 22:00:00 is 24 hour clock, 02:00:00 could be either though couldn't it?
add the date as well, or test for the negtive and add 12 hours.
Quick solution from me: add a day to the later date.
echo gmdate('h', strtotime('1970-00-01 02:00:00') - strtotime('1970-00-00 22:00:00'));
But Blender's answer seems to be easier. :)
Related
I want to display content from the database with dates up to 2hours ahead of time.
Example:
2018-11-09 20:00:00.000000
2018-11-08 19:00:00.000000
2018-11-06 19:00:00.000000
2018-11-06 18:00:00.000000
Lets say the time and date is
Nov 6th at 6pm. I want the bottom two entries to be displayed and the two future dates to not show until the current time is within 2hours of that time.
My code is as follows:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= strtotime('-2 hours')) {
echo $row3['MissionTime']."<br>";
}
I've tried several different ways but I can't seem to get this to work right. Help and tips?
The reason your code doesn't work is that strtotime returns a number of seconds since the unix epoch. When you subtract two results of strtotime you will get a number of seconds difference which is as you expect. However you cannot compare that value to strtotime('-2 hours') as the output of that will be the timestamp for 2 hours before now (which right now is 1541539906), so the test will always pass. You should just compare it to 7200 instead (I'm pretty sure based on your question description that +7200 is more appropriate than -7200). so change
if($cT <= strtotime('-2 hours')) {
to
if($cT <= 7200) {
Note that it is almost certainly better to do this in your query. Try adding a condition on your time column as something like
WHERE MissionTime <= NOW() + INTERVAL 2 HOUR
And then you won't need to check in the PHP at all.
strtotime() returns a timestamp in seconds. Subtracting two timestamps gives you a difference between those two timestamps, in seconds.
So if strtotime($row3['MissionTime']) is a timestamp that's 1.5 hours in the future, and you subtract strtotime("now") from it, you end up with a difference of 5400 seconds (60 seconds * 60 minutes * 1.5 hours).
strtotime('-2 hours') gives you the timestamp for 2 hours ago, which is currently somewhere around 1.5 billion. This is not very useful for your situation.
Here are two ways to modify your code:
$cT = strtotime($row3['MissionTime']) - strtotime("now");
if($cT <= 7200) {
echo $row3['MissionTime']."<br>";
}
If the difference between $row['MissionTime'] and now is less than 7200 seconds (60 seconds * 60 minutes * two hours), $row3['MissionTime'] is either in the past or it's within the next two hours.
Alternatively:
if(strtotime($row3['MissionTime']) <= strtotime('+2 hours')) {
echo $row3['MissionTime']."<br>";
}
Basically the same, but perhaps more readable if you're not planning to use $cT for anything else. This simply checks if $row3['MissionTime'] is earlier than whatever time it will be in +2 hours.
I have been banging my head with this for a while now. Does someone know how I can check to see if $theDate is within the range of 24 hours FROM NOW.
This is the code I have come up with but it doesn't seem to work :(
if (time() <= ($theDate + 86400)) {
// current time is 86400 (seconds in 24 hours) or less than stored time
}
Note: $theDate is unix timestamp.
Any help would be appreciated.
Thanks :)
You have it backwards:
if ($theDate <= (time() + 86400)) {
// current time is 86400 (seconds in 24 hours) or less than stored time
}
Do you always want the range to be 24 hours? Remember that there are sometimes 23 or 25 hours in a day when changing from/to DST.
A method that will take that into account is this:-
$tomorrow = (new \DateTime())->add(new \DateInterval('P1D'));
if((new \DateTime())->setTimestamp($theDate) < $tomorrow){
//Do something
}
See the DateTime manual for more information on these very useful classes.
If I got a unix time which is e.g
1407050129
How do I get the 12:00AM of that unix day in unix timestamp , means the first minute of the day of that unix time.
Example if i want get today first minute
$today_first_min = strtotime("00:00:00");
Try this:
$that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
See demo
An alternate method to arrive at the same result... Unix time is a count of seconds since 1970-01-01 00:00:00 UTC, so each whole day is a multiple of (60*60*24) seconds.
Using this fact you can use the modulus operator (%) to calculate and then remove the remainder (ie. the seconds, hours and minutes) from the day, and get back to the first hours!
date_default_timezone_set('UTC');
$that_date = 1407050129;
$first_hour = $that_date - ($that_date % (60*60*24));
print date('Y-m-d H:i:s', strval($first_hour));
// 2014-08-03 00:00:00
I'm dealing with an api which is returning the date time stamp exactly as follows:
Mon, 14 May 2012 14:14:11 +0000
I would like process this so php works out how many minutes ago this was if the number of minutes is less than 60 else how many hours ago it was if the number of hours is less than 24 else the number of days.
Dates will never be older than a few weeks.
Thanks.
You want to use the DateTime class. It can parse that date.
$now = new DateTime('now');
$dt = new DateTime('Mon, 14 May 2012 14:14:11 +0000');
$interval = $now->diff($dt);
$minutes = $interval->format('%i');
Note that 'now' will be in your current time zone so you might want to pass the DateTimeZone parameters as well. More info is here: http://php.net/DateTime
The class should already be built into your PHP. You won't need to include it.
ok well i have this error with a counter i made witch is suppost to count down to 0
$time=1283593330+(60*15);
$time3= time();
$time2=$time-$time3;
1283593330=Sat, 04 Sep 2010 09:42:10 GMT
Error is this:
when the $time3 timestamp hit the timestamp for $time it says 05:00:00 instande of 00:00:00.
This is the code i use to call it.
Time left: '.date('g:i:s ',$time2).'<br />
im not sure if im doing something wrong, if 5 is the main time for unix_timestamp or the date commend in PHP
is there anyway to fix this? or is this from of timestamp just that bad of a idea for what i need.
Your time zone is 5 hours ahead of UTC: you say 1283593330 is 09:42, but it's actually 04:42 UTC.
When $time2 is zero, this represents the Unix time epoch: this is midnight UTC on 1st January 1970. So when you output this using date, it shows that time in your time zone: 00:00 UTC which is 05:00 in your time zone.
What's important is that $time2 is zero when the target time is reached.
Given that your counter is counting down 15 minutes, you can get the remaining time like this:
$hours = floor($time2 / 60);
$mins = $time2 % 60;
printf("Time left: %d:%02d\n", $hours, $mins);