skip to Main Content

How in php I can convert 1 digit, e.g:

0
1
2
...
17
18
19

To a full time representation like:

00:00:00
01:00:00
02:00:00
...
17:00:00
18:00:00
19:00:00

I tried working with strtotime() and date() but didn’t really get the hang out of it

3

Answers


  1. Chosen as BEST ANSWER

    Seems like I was able to fix it myself, for anyone looking for a similar situation. The solution would be:

    date('H:i:s', '15' * 60 * 60);
    

    the output for this is:

    15:00:00


  2. You could also use the mktime() function, like this:

    echo date('H:i:s', mktime(15, 0, 0));
    

    returns:

    15:00:00
    

    Sample Output: https://3v4l.org/vuenr

    Login or Signup to reply.
  3. sprintf with zero padding to two digits:

    foreach (range(0, 23) as $i) {
       echo sprintf('%02d:00:00' . PHP_EOL, $i);
    }
    

    Fiddle here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search