Convert time to read format

I have a time like this in the database

                [open_time] => 10:00:00
                [close_time] => 23:00:00

I want to convert it to a readable form, for example, 10:00 a.m. 11:00 p.m.

I tried this:

$open = date("g:s a",$time['open_time']);

$close = date("g:sa",$time['close_time']);

I get the following error:

Invalid numeric value

+3
source share
3 answers

dateexpects an integer argument, a traditional Unix timestamp .

Try the following:

date('g:s a', strtotime($time['open_time']));

strtotimetrying to convert a string to a whole Unix timestamp using expected with date.

+10
source

If this is in your database, consider MySQL DATE_FORMAT () directly in your query

+2

UNIX ( ):

http://php.net/manual/en/function.date.php

timestamp

Extra timestamp A parameter is an Unix integer timestamp that defaults to the current local timestamp if no timestamp is specified. In other words, the default value is time ().

<?php
$t = time();
print_r($t);
print_r(date('g:i a', $t));
?>

gives

1288935001
10:30 pm
+2
source

Source: https://habr.com/ru/post/1773183/


All Articles