fred2
fred2

Reputation: 1110

Strange behaviour with mktime producing error in June/July

I have the following ungainly code to turn a date string into another date string.

//$invDate starts as a date string in format dd/mm/yyyy
$dateArray = explode('/', $invDate);      
$invDate = $dateArray[0] .' '.  date("F",mktime (1,1,1,$dateArray[1])) .' '. $dateArray[2];

I'm not particularly proud of it, but it produces an unambiguous date in a country where both US and UK methods of doing dates can produce confusion.

It's worked fine for ages, and then suddenly today it has started turning

01/06/2012

into

1 July 2012

I've looked at how mktime behaves, and can't see any reason why mktime (1,1,1,6) should ever produce a date in July. Any ideas?

Upvotes: 3

Views: 144

Answers (1)

Paul
Paul

Reputation: 141827

This is happening because mktime defaults to the current time for missing fields. Since you didn't specify a day, and today is May 31st it is assuming June 31, which doesn't exist so it wraps around to July. Specify a day by adding a fifth argument to mktime:

date("F", mktime(0, 0, 0, $dateArray[1], 1))

Upvotes: 5

Related Questions