user1723710
user1723710

Reputation: 61

Wrong output from mktime() in PHP

I am trying to set up a php where that it sets the date up for the 1st September for each year. I am using CRON to state that everytime the date is 7th September, then the php will actually update the database for rows whose dates ae a week before that 7th September, (ist September).

Now the code below I tested for the 28th October and when I echo $selectedDate it outputs 2012-10-28 which is fine. But when I change the date to 1st September, it outputs `2011-12-01 which is obviously incorrect. it should output `2013-09-01 as the next September date will be in 2013. Then after the Ist September date has passed in 2013, then the year should change to 2014 and etc.

How can I get the correct date to be outputted?

Below is code:

$createDate = mktime(0,0,0,09,01,date("Y"));
$selectedDate =  date('Y-m-d', ($createDate));

Upvotes: 0

Views: 113

Answers (1)

Madara's Ghost
Madara's Ghost

Reputation: 175098

That's because 09 is interpreted as an octal 9, which is invalid.

It works as expected when you pass 9 instead of 09:

$createDate = mktime(0,0,0,9,1,date("Y"));
//                         ^ ^  No preceeding 0s.
$selectedDate =  date('Y-m-d', ($createDate));

var_dump($createDate, $selectedDate);

Upvotes: 2

Related Questions