4

Hi I am trying to take a total amount of seconds i.e. 972 seconds and convert it into this format:

minutes:seconds:milliseconds

I can't find a PHP function such as gmdate() for time that will help me convert it. Please help!

Thank You.

user1985436
  • 65
  • 1
  • 2
  • 5
  • 3
    How do you plan to get miliseconds from seconds ? – Glavić Jan 16 '13 at 23:05
  • I think the answer possibly lies within this Question. [http://stackoverflow.com/questions/4763668/php-convert-milliseconds-to-hours-minutes-seconds-fractional][1] [1]: http://stackoverflow.com/questions/4763668/php-convert-milliseconds-to-hours-minutes-seconds-fractional – Ian Brindley Jan 16 '13 at 23:08
  • This answer could be valuable as well: https://stackoverflow.com/a/37336227/1748266 (shameless plug...) – MER Apr 23 '20 at 18:07

3 Answers3

4

This works for PHP 5 >= 5.3.0

$date = DateTime::createFromFormat('U.u', microtime(TRUE));
var_dump($date->format('Y-m-d H:i:s.u'));

From DateTime with microseconds

Manual: http://php.net/manual/de/datetime.createfromformat.php

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
1

In the: http://php.net/manual/en/function.date.php you can use the format: date("i:s:u"); in PHP > 5.2.2,

i: minutes, s:seconds, u:microseconds and use a timestamp in the second argument to set as you were saying 972 seconds -> 0:972:0 But if you only want milliseconds I think you would have to manipulate the resultating string with substr,...

paido
  • 76
  • 4
  • 1
    that shows in that format, however it's not returning the accurate value of milliseconds. So I tried: echo date("i:s:u", 972); and it returns: 16:12:000000 – user1985436 Jan 20 '13 at 06:54
  • Yeh i have this issue as well and have yet to find a workaround. :( – Bonxy Mar 05 '16 at 13:36
  • From manual: `Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.` (might be added after this question) – PiTheNumber Feb 22 '18 at 08:39
1

Try This

$second = "123456789"
$s = $second%60;
$m = floor(($second%3600)/60);
$h = floor(($second%86400)/3600);
$d = floor(($second%2592000)/86400);
$M = floor($second/2592000);

echo "$M months, $d days, $h hours, $m minutes, $s seconds";
Shoaib Quraishi
  • 232
  • 2
  • 17