0

anyone know whats the problem/reason with this codes?

1. calculation with fix time
<?php
date_default_timezone_set('UTC');
$deff = strtotime("20:00:00")-strtotime("19:00:00");
echo date('h:i', $deff );

OUTPUT = 01:00

//above was correct
?>

2.output should be in munite, but the output is 12:50 what's wrong with this code?

date_default_timezone_set('UTC');
$deff = strtotime("20:00:00")-strtotime("19:10:00");
echo date('h:i', $deff );

OUTPUT = 12:50
INSTEAD OF = 00:50

anyone help would greatly appreciated..

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by edwinhermann
0

Strange... I have just tried your second example and it is giving me 00:50.

date_default_timezone_set('UTC'); 
$difference = strtotime('20:00:00') - strtotime('19:10:00');
echo date('H:i', $difference);

Have you tried adding a full date before the time to see if it makes any difference?

date_default_timezone_set('UTC'); 
$difference = strtotime('2012-01-01 20:00:00') - strtotime('2012-01-01 19:10:00');
echo date('H:i', $difference);

String to time converts the string into a unix timestamp, which as I expect you know is the number of seconds since 01/01/1970. As you're only providing a time, it could be a bug with the version of PHP you're using.

I am running PHP 5.3.8. What are you using?

1

Neither of these are the correct approach. strtotime turns the date into an integer. You then subtract one integer from the other which gives you a difference. You are then trying to turn that difference into a date (which will result in a date close to the epoch depending in the size of the difference).

You should be using DateTime::diff - see http://php.net/manual/en/datetime.diff.php There are worked examples there.

Votes + Comments
good
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.