Date returning different results in different php versions

I have a date Aand B.

I wanted to get hours / minutes between them. How:

date('h:i', strtotime(B) - strtotime(A));

But I get weird results:

echo date('h:i', strtotime('2014-01-01') - strtotime('2014-01-01'));
// echoes: 01:00 (!)


date_default_timezone_set('Europe/London');
$date = new DateTime();
$A = $date->format('Y-m-d H:i:s'); echo '<br />';
$date->modify("+64 minutes");
$B = $date->format('Y-m-d H:i:s'); echo '<br />';
echo date('h:i', strtotime($B) - strtotime($A));
// echoes 02:04 (!)

Live example for the previous code:

Why is this and how to get the expected result?

+4
source share
2 answers

This is the correct behavior.

Why? Think about it: strtotime('2014-01-01') - strtotime('2014-01-01') zero , but date()expect a timestamp as a second parameter. Thus, you are trying to get a date from time zero. And this moment is different in different time zones. Your LondonTZ has an offset +01, so a 0-point timestamp 01 Jan 1970 01:00:00- and therefore date('h:i', 0)there is01:00

, , Moscow:

date_default_timezone_set('Europe/Moscow');
$date = new DateTime();
$A = $date->format('Y-m-d H:i:s'); 
$date->modify("+64 minutes");
$B = $date->format('Y-m-d H:i:s'); 

echo date('h:i', strtotime($B) - strtotime($A));//04:04

- 04:04 - - +03 ( 03 + 64 )

+1

date , , . h:i , : , , , , , . , date - . , .

+1

All Articles