Hướng dẫn dùng time diff trong PHP

How to calculate minute difference between two date-times in PHP?

S.L. Barth

8,08971 gold badges50 silver badges63 bronze badges

asked Dec 13, 2008 at 13:05

Tom SmykowskiTom Smykowski

24.8k52 gold badges155 silver badges227 bronze badges

The answers above are for older versions of PHP. Use the DateTime class to do any date calculations now that PHP 5.3 is the norm. Eg.

$start_date = new DateTime['2007-09-01 04:10:58'];
$since_start = $start_date->diff[new DateTime['2012-09-11 10:25:00']];
echo $since_start->days.' days total
'; echo $since_start->y.' years
'; echo $since_start->m.' months
'; echo $since_start->d.' days
'; echo $since_start->h.' hours
'; echo $since_start->i.' minutes
'; echo $since_start->s.' seconds
';

$since_start is a DateInterval object. Note that the days property is available [because we used the diff method of the DateTime class to generate the DateInterval object].

The above code will output:

1837 days total
5 years
0 months
10 days
6 hours
14 minutes
2 seconds

To get the total number of minutes:

$minutes = $since_start->days * 24 * 60;
$minutes += $since_start->h * 60;
$minutes += $since_start->i;
echo $minutes.' minutes';

This will output:

2645654 minutes

Which is the actual number of minutes that has passed between the two dates. The DateTime class will take daylight saving [depending on timezone] into account where the "old way" won't. Read the manual about Date and Time //www.php.net/manual/en/book.datetime.php

answered Sep 12, 2012 at 7:11

17

Here is the answer:

$to_time = strtotime["2008-12-13 10:42:00"];
$from_time = strtotime["2008-12-13 10:21:00"];
echo round[abs[$to_time - $from_time] / 60,2]. " minute";

answered Dec 13, 2008 at 13:36

user38526user38526

3,7532 gold badges14 silver badges2 bronze badges

4

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

Noman

1,3902 gold badges17 silver badges38 bronze badges

answered Dec 13, 2008 at 13:23

OliOli

231k62 gold badges216 silver badges294 bronze badges

3


answered Dec 13, 2008 at 15:49

TomTom

8,98229 gold badges129 silver badges226 bronze badges

3


Output:

75

answered Sep 12, 2019 at 14:10

0

It worked on my programs, i'am using date_diff, you can check date_diff manual on here.

$start = date_create['2015-01-26 12:01:00'];
$end = date_create['2015-01-26 13:15:00'];
$diff=date_diff[$end,$start];
print_r[$diff];

You get results what do you want.

answered Jul 9, 2015 at 15:43

yussanyussan

2,2501 gold badge19 silver badges24 bronze badges

2

another way with timezone.

$start_date = new DateTime["2013-12-24 06:00:00",new DateTimeZone['Pacific/Nauru']];
$end_date = new DateTime["2013-12-24 06:45:00", new DateTimeZone['Pacific/Nauru']];
$interval = $start_date->diff[$end_date];
$hours   = $interval->format['%h']; 
$minutes = $interval->format['%i'];
echo  'Diff. in minutes is: '.[$hours * 60 + $minutes];

answered Dec 24, 2013 at 13:16

MuhammadMuhammad

3,0795 gold badges39 silver badges68 bronze badges

1

I wrote this function for one my blog site[difference between a past date and server's date]. It will give you an output like this

"49 seconds ago", "20 minutes ago", "21 hours ago" and so on

I have used a function that would get me the difference between the date passed and the server's date.


Save it as a file suppose "date.php". Call the function from another page like this

Chủ Đề