Harsha M V
Harsha M V

Reputation: 54949

Convert time to readable format

I have time like this in the database

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

I want to convert it into readable form like 10:00am 11:00pm

I tried this:

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

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

I'm getting the following error:

A non well formed numeric value encountered

Upvotes: 3

Views: 4306

Answers (3)

nonopolarity
nonopolarity

Reputation: 151006

The second argument needs a UNIX timestamp (epoch time):

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

timestamp

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

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

gives

1288935001
10:30 pm

Upvotes: 2

Pascal Qyy
Pascal Qyy

Reputation: 4502

If it's in your database, consider using the MySQL DATE_FORMAT() function directly in your request

Upvotes: 2

user229044
user229044

Reputation: 239311

date expects an integer argument, the traditional Unix timestamp.

Try this:

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

strtotime attempts to convert a string into an integer Unix timestamp as expected by date.

Upvotes: 9

Related Questions