1

I have a total ammount of milliseconds (ie 4007587) and I want to display it as hours:minutes:seconds.

my PHP code:

$mil = 4007587
$timestamp = $mil/1000;
echo date("g:i:s", $timestamp);

the result should be 1:06:47 but my result is 8:06:47 what's wrong?

Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39
  • 2
    Possible duplicate of [Output is in seconds. convert to hh:mm:ss format in php](https://stackoverflow.com/questions/3534533/output-is-in-seconds-convert-to-hhmmss-format-in-php) – Tim Biegeleisen Sep 02 '18 at 07:46
  • Check your server timezone. – Piyush Bansal Sep 02 '18 at 07:48
  • As a general rule when working with dates, ignore the old [date & time PHP functions](http://php.net/manual/en/ref.datetime.php) and use the [`DateTime`](http://php.net/manual/en/book.datetime.php) **classes**. With a couple of exceptions, the old date&time functions do not work with timezones and handling multiple timezones with them is difficult. The classes work with timezones and each `DateTime` object knows the timezone of the date it stores. Working with them is easy and produces more readable code. – axiac Sep 02 '18 at 08:08

1 Answers1

4

It's because date takes account of your local timezone. Try using gmdate instead:

$mil = 4007587;
$timestamp = $mil/1000;
// local timezone
echo date("g:i:s", $timestamp);
// UTC
echo gmdate("g:i:s", $timestamp);

Output

2:06:47
1:06:47
Nick
  • 138,499
  • 22
  • 57
  • 95