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..

Recommended Answers

All 2 Replies

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?

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.

commented: good +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.