views:

80

answers:

4

Take a look on the code below:

$t=77;
date("H:i:s", $t);

It returns

21:01:17

The correct result, of course, should be something like 00:01:17.

The $t value indeed is a value in seconds returned by the YouTube GData API, trought($videoEntry->getVideoDuration()).

How can this problem be fixed?

A: 

Try to set GMT timezone.

date_default_timezone_set('Europe/London');
Sergey Kuznetsov
Isn't London on a different time than GMT some parts of the year?
Atli
Setting to Europe/London returns 01:01:17. See solution below.
Paulo Bueno
+2  A: 

The second argument to date() is a unix timestamp - in other words it is a number of seconds since Jan 1, 1970, adjusted to what PHP is set to for a timezone (can be set with date_default_timezone_set).

tloach
+8  A: 

date is timezone specific. You need to set it to GMT to get the results you want.

date_default_timezone_set('GMT');
$t=77;
echo date("H:i:s", $t);
Atli
Ok GMT or UTC works fine. thx
Paulo Bueno
A: 

Hey hi,

I think if you getting values in second, then you should use mktime function then it will give correct result. For eg.:

$t=77; echo date("H:i:s", mktime(0,0,$t));

Gaurav Padia
Hey. The `mktime` function creates a timestamp - the number of seconds since `1970-01-01`- and using it with only a seconds value simply creates a timestamp for today, at that specific time. E.g. `2009-12-01 00:01:17` *(using GMT)*. Which, if you are only interested in the time values, changes nothing. What you posted is effectively equivalent to `$t=77; echo date("H:i:s", $t);`.
Atli
Hi Atli... you are right... thanks for clarification
Gaurav Padia