I want to calculate the difference between two times, one of which is the current time, and the other is just in the format HH:MM, always in the future.
If I just subtract $futuretime from $now, it should, of course, be a positive number.
This works fine until...
If $now is in the afternoon or evening and $futuretime is, say, 7AM next morning, how can I force it to understand the the time is always going to be in the future?
(It's for working out the time of something that occurs about every half an hour during working hours, and then stops until the following morning)
Thanks in advance!
Simply add a whole day to the difference if it is negative. Assuming that $now and $futuretime are timestamps (stored in seconds since midnight), simply do the following:
$diff = $futuretime - $now;
if ($diff < 0)
$diff += 24 * 3600;
If they are in HH:MM format:
list($future_hours, $future_mins) = explode(":", $futuretime);
$futuretime = $future_hours * 60 + $future_mins;
list($now_hours, $now_mins) = explode(":", $now);
$now = $now_hours * 60 + $now_mins;
$diff = $futuretime - $now;
if ($diff < 0)
$diff += 24 * 60;
Of course the latter case returns the difference in minutes, while the former returns the difference in seconds.
Edit: as Andy noted below in the comments, this is not a good solution if you care about changes in DST. See his solution for a better approach.
If you're on PHP 5.3+, use PHP's DateTime:
$d1 = new DateTime('14:52:10');
$d2 = new DateTime('12:12:10');
$diff = $d1->diff( $d2 );
var_dump( $diff );
You can simply convert both dates to timestamp, do the calculations and convert it to required format, i.e. days, hours and minutes. it's quite simple.
Related
i try to make time difference with carbon
$dt = Carbon::parse('2018-07-15 00:00:00');
$now = Carbon::now('Asia/Dubai'); //set current time
$seconds = $now->diffInSeconds( $dt ); //difference turn into second
$days = $dt->diffInDays($dt->copy()->addSeconds($seconds));
$hours = $dt->diffInHours($dt->copy()->addSeconds($seconds)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($seconds)->subHours($hours));
$days result are 12 (its right).
$hours result are 8 (seems not right).
$minutes result are 17299 (clearly wrong).
how to get the result for example 12 day 5 hours 45 minutes
Actually functions like diffInSeconds give total difference in seconds that's why the number is so large,to get the minutes for the time difference right you can use -:
$minutes = ($now->minute - $dt->minute);
Is there a nice simple shorthand way of finding out how many seconds past midnight a certain datetime is? Not how many seconds it is away from now, the seconds past in that day
eg:
2015-04-10 00:00:00 should returns 0
2015-04-12 09:20:00 should returns 33600
2015-04-14 15:20:00 should returns 55200
Is there a nice short method of doing this?
Here is a simple code for you.
Any day code
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime(date("Y-m-d 00:00:00", strtotime($date)));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
Current day code
<?php
$midnight = strtotime("midnight");
$now = date('U');
$diff = $now - $midnight;
echo $diff;
?>
Maybe a more clean code would be to use strtotime("midnight", <EpochTime>);
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime("midnight", strtotime($date));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
A day is 24 hours is 60 minutes is 60 seconds is 1000 miliseconds, so timestamp % (24*60*60*1000) could work, maybe, at least if you are on the same time zone as the epoch php is using (or whatever you timestamp is coming from). It would work at least for nothing too demanding of accuracy. PHP uses your system's time, so many things will break the idea (change the clock, DST, leap seconds, etc)
I would personally use timestamp - startofday timestamp and calculate using the language's calendar instead.
I've viewed a few posts on this, but I'm still a bit confused on how it works.
I want to compare 2 times. The first time is the time NOW and the second time is a datetime coming from the database. I'm able to get the difference in days, months and years, but when I try to get the mins and seconds, the numbers do not seem correct.
Here is my code for getting the time in minutes and seconds:
$date1 = date('Y-m-d H:i:s');
$date2 = $blah['datetime']; //the variable here is in dateTime format coming from the database
$diff = abs(strtotime($date2) - strtotime($date1)); //Does this give the seconds?
$mins = floor($diff / 60);
So far minutes it's returning something like 560, even though I added that row (in the database to a table) a minute ago. Perhaps I'm misunderstanding something, or missing something. Let me know if you want me to clarify anything. Thanks!
This calculation is quite easy with PHP's DateTime and DateInterval classes.
$date1 = new \DateTime();
$date2 = \DateTime::createFromFormat('Y-m-d H:i:s', $blah['datetime']);
$diff = $date1->diff($date2); // $diff is an Instance of DateInterval
$mins = ($diff->days * 60 * 24) + ($diff->h * 60) + $diff->i;
I am trying to compare times and to see if it is less than a set time and if so, do something...How can this be achieved?
For example I have this so far.
$now = strtotime(date('g:i a'));
$event_time = strtotime("10:00 am");
With these two variables, how can I check if the event_time is 5 minutes BEFORE the current "now" time?
Thanks...
You don't need strtotime for the current time. Timestamps are values in seconds, just do some math:
$now = time();
$event_time = strtotime("10:00 am");
if( ($now - $event_time) == (5 * 60)) // 5 minutes * 60 seconds, replace with 300 if you'd like
{
// 5 minutes before
}
Demo - But you should use the above code for the $now variable.
How to get duration in terms of minutes by subtracting a previous time stamp from the present time in PHP?
The format of time stamp is like
2009-12-05 10:35:28
I want to calculate how many minutes have passed.
How to do it?
To do this in MySQL use the TIMESTAMPDIFF function:
SELECT TIMESTAMPDIFF(MINUTE, date_lastaccess, NOW()) FROM session;
Where session is the table and date_lastaccess is a DATE / DATETIME field.
If you don't wanna add a library, you can do this pretty easily:
<?php
$date1 = "2009-12-05 10:35:28";
$date2 = "2009-12-07 11:58:12";
$diff = strtotime($date2) - strtotime($date1);
$minutes_passed = $diff/60;
echo $minutes_passed." minutes have passed between ".$date1." and ".$date2;
?>
Check out some pretty date libraries in PHP. Might have something for you. Here's one : http://www.zachleat.com/web/2008/02/10/php-pretty-date/
strtotime("now") - strtotime("2009-12-05 10:35:28")
That will get you seconds difference. Divide by 60 for minutes elapsed. If you have any information on the time zone of the initial timestamp, however, you'd need to adjust the "now" to a more comparable time
something like that
$second = strtotime('now') - strtotime($your_date);
$minute = $second / 60;
strtotime return the number of seconds since January 1 1970 00:00:00 UTC, so you can easily manipulate this. if you need don't want to have 35.5 minute you can use floor to round up the number. Last remark both time need to be in the same timezone or you are going to count the timezone difference also.
You could just use
$timestamp = time();
$diff = time() - $timestamp
$minutes = date( i, $diff );
function timeAgoMin($timestamp){
if(!is_int($timestamp)){
$timestamp = strtotime($timestamp);
}
return ((time() - $timestamp) / 60);
}