Jage
Jage

Reputation: 8096

How to make PHP date() ignore local GMT setting?

I am storing the time of day as a number of seconds since midnight. I have a number that should be 8:00 am:

//3600 secs / hour * 8 = 28800

$time = 28800;
var_dump(date('h:i a', $time ));

My output is:

string(8) "01:00 am"

Based on my location, I am -7:00 GMT, so I can see where I would get 1:00 am, but how do I do format this time to show 8:00 am, essentially making it ignore the current GMT setting while formatting this time?

Upvotes: 0

Views: 267

Answers (2)

fayhot
fayhot

Reputation: 59

two ways.

first you may try gmdate() function which output the raw GMT time .

and the other way you can set timezone before you use date function.

as follow .

date_default_timezone_set('Asia/Shanghai');
echo date('H:i:m', time());

Upvotes: 1

Jage
Jage

Reputation: 8096

I figured this out. The solution is to use gmdate(). It will format a raw timestamp to GMT.

Upvotes: 0

Related Questions