Dan
Dan

Reputation: 3867

PHP mktime giving dates 12 off

I am writing a function to convert an internal date format into a timestamp value. However, when I print the date out in YYYY-MM-DD HH format the date is 12 hours off.

The code below gives me the wrong date and time. I am expecting 2011-03-25 13 but instead I am getting 2011-03-25 01.

 date_default_timezone_set("Europe/London");
 $epoch = mktime(13,0,0,3,25,2011);
 echo date('Y-m-d h', $epoch);

When I use the following code I expect 2001-02-01 01 and get what I expected.

 date_default_timezone_set("Europe/London");
 $epoch = mktime(1,0,0,2,1,2011);
 echo date('Y-m-d h', $epoch);

It seems that the 12 hour offset starts on March 25th in the 13th hour.

Any idea why this happens and how do I keep it from happening? Does this have to do with the different day light savings dates? The server timezone is set to "America/Los_Angeles".

Upvotes: 0

Views: 1239

Answers (1)

hakre
hakre

Reputation: 197775

It works, you're just using the wrong format code, take H (24 hour format) instead of h (12 hour format):

date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d H', $epoch);

Read the PHP Manual, it explains each code in detail.

Upvotes: 1

Related Questions