How to Get Time Difference in Minutes in PHP

How to get time difference in minutes in PHP

Subtract the past most one from the future most one and divide by 60.

Times are done in Unix format so they're just a big number showing the number of seconds from January 1, 1970, 00:00:00 GMT

Difference between timestamps in minutes in PHP

Try using unix timestamp. Practically it measures the time in seconds from 1/1/1970 and it's a lot easier to use and understand than a php object.

$currentTimestamp = new DateTime()->getTimestamp();
$userLastActivity = date($date)->getTimestamp();
$timeLapse = (($currentDate - $userLastActivity)/60);

You should have the time saved as timestamp on the server too, in that case you could use the $date directly as a number, with no need for a conversion. And also, because it's universal, you can pass it around to javascript or any other language without any worries for conversion

Number of Minutes between two dates

One method:

$minutes = (strtotime("2012-09-21 12:12:22") - time()) / 60;

strtotime converts the date to a Unix timestamp - the number of seconds since the Unix epoch. Subtract the current timestamp and you have the number of seconds between the current time and the future time. Divide by 60 and the result is in minutes.

If you don't know for certain the time you're comparing is in the future, take the absolute value to get a positive number:

$minutes = abs(strtotime("2012-09-21 12:12:22") - time()) / 60;

Just to be complete in my answer, there is a more elaborate OO approach available in PHP:

$time = new DateTime("2012-09-21 12:12:22");
$diff = $time->diff(new DateTime());
$minutes = ($diff->days * 24 * 60) +
($diff->h * 60) + $diff->i;

This is especially useful if the input time is from a time zone other than the server's.

get time difference in hours minutes and seconds

Use DateTime() with DateInterval()

$expiry_time = new DateTime($row['fromdb']);
$current_date = new DateTime();
$diff = $expiry_time->diff($current_date);
echo $diff->format('%H:%I:%S'); // returns difference in hr min and sec

PHP get time difference in minutes

$datetime1 = new DateTime();
$datetime2 = new DateTime($row['eventTime']);
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%i minutes');
echo $elapsed;

Time diff in minutes between 2 dates

Instead of DateTime you can use strtotime and date:

$datetime1 = strtotime("2011-10-10 10:00:00");
$datetime2 = strtotime("2011-10-10 10:45:00");
$interval = abs($datetime2 - $datetime1);
$minutes = round($interval / 60);
echo 'Diff. in minutes is: '.$minutes;

Find time difference in minutes with php or mysql

You could use diff within DateTime.

#!/usr/bin/env php

<?php

$datetime1 = new DateTime('13:00');
$datetime2 = new DateTime('14:40');

$interval = $datetime1->diff($datetime2);

$hours = $interval->format('%h');
$minutes = $interval->format('%i');

echo $hours * 60 + $minutes;

?>


Related Topics



Leave a reply



Submit