Steven Schmieg
Steven Schmieg

Reputation: 49

PHP - Converting an integer to a date time

I'm trying to convert a variable with an integer value to a 12 hour format, but It's just returning 12 am.

$hour = 18;

echo date('h a', $hour);

What am I doing wrong here?

Upvotes: 1

Views: 108

Answers (1)

F.Igor
F.Igor

Reputation: 4350

You can use:

$hour = 18;
echo date('h a', strtotime("$hour:00:00"));

Beacause date(format,time) expects a unix timestamp (seconds since the Unix Epoch, January 1 1970 00:00:00 GMT). Actually, using 18 will get '00:00:18' which is 12:18 am

Also, this will work:

$hour = 18;
echo date('h a', $hour*3600 ); // 1hr=3600sec

Upvotes: 3

Related Questions