3

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
fred2
  • 1,015
  • 2
  • 9
  • 29
  • I should add that, pretty obviously I could create an array of months and not use mktime, but I'm interested to see if I've made a mistake, or it's a bug. – fred2 May 31 '12 at 20:38

1 Answers1

5

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))
Paul
  • 139,544
  • 27
  • 275
  • 264