I have a variable $minutes that expresses a point in time like this: 2011-08-31 21:02:15
How could I subtract it from the present time, and express the difference in minutes?
$minutes = (int)((time()-strtotime($minutes))/60);
Example Here
You could use the DateTime and DateInterval classes.
$now = new DateTime();
$then = new DateTime("2011-08-31 21:02:15");
$minutes = $now->diff($then)->i;
If you have a UNIX timestamp instead of a string, you can do this instead:
$now = new DateTime();
$then = new DateTime();
$then->setTimestamp(myTimestamp);
$minutes = $now->diff($then)->i;
Related
I have mysql timestamps named the following:
$end_time = $row['end_time'];
$paused_time = $row['paused_time'];
$current_time = date("Y-m-d H:i:s");
i need to get the difference between the current time and the paused time, then add that total to the end time.
Essentially what im trying to achieve is when an event is paused, it updates the $paused_time in the database, so when i resume again i need to add the time since it was paused to the end time which will increase it.
say i paused it 1 hour ago, the difference between $paused_time and $current_time is 1 hour, so $end_time will be $end_time + 1 hour which i will then update the database replacing the current end time.
Everything i try is such a mess and not really achieving my goal so i would appreciate how to go about doing this.
$datetime1 = new DateTime($paused_time);
$datetime2 = new DateTime($current_time);
$interval = $datetime1->diff($datetime2);
// add $interval to $end_time??
Many thanks and hope i explained it clear enough
For example:
$end_time = "2019-07-07 14:09:07";
$paused_time = "2019-07-07 13:09:07";
$current_time = date("Y-m-d H:i:s");
$datetime1 = new DateTime($paused_time);
$datetime2 = new DateTime($current_time);
$datetime3 = new DateTime($end_time);
$interval = $datetime1->diff($datetime2);
$sumDateTime = $datetime3->add($interval);
Can you try it with strtotime()? You will get milliseconds out of it and you can convert it back to any unit of time you wish.
$end_time = strtotime($row["end_time"]);
$paused_time = strtotime($row["paused_time"]);
$current_time = time();
$interval = $end_time - $paused_time;
$add_interval_to_end_time = $end_time + $interval;
Cheers
Hanns
Is there any native function for getting the difference between two timestamps? I want to get minutes difference of two timestamps.
Edit: Sorry but actually I just want the difference of two Unix timestamp.
$date = new DateTime();
$date1_timestamp = $date->getTimestamp();
sleep('120');
$date2_timestamp = $date->getTimestamp();
function get_unix_timestamp_minutes_difference($start, $end) {
/*
Some code for return the difference between two unix timestamps
*/
}
echo get_timestamp_minutes_difference($date1_timestamp, $date2_timestamp);
You can use Carbon class http://carbon.nesbot.com/docs/ and use his difference API :http://carbon.nesbot.com/docs/#api-difference
Try This:
$datetime1 = strtotime("2017-05-16 10:00:00");
$datetime2 = strtotime("2017-05-16 10:45:00");
$interval = abs($datetime2 - $datetime1);
$minutes = round($interval / 60);
echo 'Diff. in minutes is: '.$minutes;
I have two Timestamps
2016-01-01 00:00:00
2016-01-02 23:59:59
Using PHP how can I calculate the number of hours and minutes between the two times and get the result as a decimal with 2 places after the .
currently I have this:
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = date_diff($Start,$Finish);
$Hours = $Interval->format('%h.%i');
But the result is incorrect if the user starts the timer on Day 1 and finishes on day 2.
You could multiply the number of days by 24 to convert them to hours, then sum the hours and concatenate the minutes:
$start = new DateTime('2016-01-01 00:00:00');
$end = new DateTime('2016-01-02 23:59:59');
$interval = $end->diff($start);
$days = $interval->format('%d');
$hours = 24 * $days + $interval->format('%h');
echo $hours.':'.$interval->format('%i');
You could format the DateTime as a UNIX timestamp, and then simply subtract to get the total seconds, and format the output with gmdate().
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = $Start->format('U') - $Finish->format('U');
$Hours = gmdate("H:i:s", $Interval);
Try this.
$Hours = $Interval->format('%a.%h.%i');
WHAT I AM DOING
I want to find the difference between 2 datetime and add it to another datetime. I am only able to get the difference in Y-m-d H:i:s format.
CODE
$begin = new DateTime($start);
$finish = new DateTime($end);
$diff = $begin->diff($finish);
$difference = $diff->format("%Y-%M-%D %H:%I:%S");
Here I want to add $difference to another datetime say $finaldate. If its not possible is there any way of getting the difference in only minutes, then i could use $date->modify("+$difference minutes");
*This is a method using DateTime:*
$begin = new DateTime($start);
$finish = new DateTime($end);
$difference = $finish->format('U') - $begin->format('U');
// working version
$minutesDiff = round(($difference/60), 0);
$finalDate = new DateTime();
$finalDate->modify(sprintf('+%s minutes', $minutesDiff));
edit
added missing bracket
edit2
version without ->diff() method
What about:
$begin = strtotime($start);
$finish= strtotime($end);
$diff = $finish-$begin;
$finaldate = strtotime($finaldate)+$diff;
echo date("Y-M-D h-i-s",$finaldate);
$time_start = mktime(12,0,0,1,1,2011);
$time_end = mktime(12,0,0,7,1,2011);
$format = '%m months';
$start_date = new DateTime(date(DATE_ATOM,$time_start));
$end_date = new DateTime(date(DATE_ATOM,$time_end));
$diff = $start_date->diff($end_date, true);
echo $diff->format($format);
Outputs "5 months", I guess because it's off by an hour due to DST. However, I need to calculate the difference in calendar months; is there another class/function that will do this?
Added some fixes:
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
$time_end += (date('I',$time_end)-date('I',$time_start))*3600; // correct for DST
$start_date = new DateTime(date(DATE_ATOM,$time_start));
$end_date = new DateTime(date(DATE_ATOM,$time_end));
$start_date->modify('12pm'); // ignore time difference
$end_date->modify('12pm');
$diff = $start_date->diff($end_date);
echo $diff->format($format);
This seems to give the results I want, but I haven't fully tested it yet.
More fixes, based on Herbert's suggestions:
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
$start_date = new DateTime();
$end_date = new DateTime();
$start_date->setTimestamp($time_start);
$end_date->setTimestamp($time_end);
$diff = $start_date->diff($end_date);
$hours = $diff->format('%h');
$mins = $diff->format('%i');
$secs = $diff->format('%s');
$start_date->setTime(12,0,0);//ignore time difference for date calculations
$end_date->setTime(12,0,0);
$diff = $start_date->diff($end_date);
$years = $diff->format('%y');
$months = $diff->format('%m');
$weeks = $diff->format('%w');
$days = $diff->format('%d');
Note that the $start_date->modify('12pm') wasn't actually doing anything at all. Not sure why it didn't error.
Update
After messing around with a lot of different ideas it occurred to me that timestamps are in GMT (UTC). date(DATE_ATOM,$time_start) is applying the default timezone. However, if you set the timestamp explicitly, DateTime will assume UTC — thus, no DST problem.
The following code seems to work regardless of timezone or DST.
<?php
$time_start = mktime(12,0,0,1,1,2011);
$time_end = mktime(12,0,0,7,1,2011)
$start_date = new DateTime();
$end_date = new DateTime();
$start_date->setTimestamp($time_start);
$end_date->setTimestamp($time_end);
$diff = $start_date->diff($end_date);
$format = '%m months';
echo $diff->format($format);
?>
I tested some edge cases — both date and time, and a variety of timezones — but I haven’t tested every possibility so if you come across an issue, I’d be interested in hearing about it.