Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any PHP function to format time (not date)?

Tags:

php

time

I have a mysql table field set as time type which stores data in the HH:mm:ss format. So when I list the data, it prints as, for example, 16:30:00. But I want to display hh:mm part only (not the seconds).

In case of datetime types, I can do date('H:i', '2010-03-16 16:30:00'). I mean I can retrieve any part. I wonder if there is any similar way like this for time only fields??

Please see, I can manipulate the time string to get rid of seconds in time part using str_replace, explode etc, I just wonder if there is any standard function there which I am not aware of.

like image 626
user187580 Avatar asked Dec 03 '22 13:12

user187580


1 Answers

If you want to do this with PHP, you'd have to get a timestamp from the time first, e.g.

echo date('H:i', strtotime('16:30:00'));

will output 16:30. Strtotime will assume the time is for the current date then.

like image 57
Gordon Avatar answered Dec 14 '22 18:12

Gordon