PHP - day and month getting transposed

Associate
Joined
18 Oct 2002
Posts
2,055
Location
Southend-on-Sea
I seem to be having a bit of a problem with dates in PHP. I’ve got a multi-dimensional array, one of the columns contains a date string. I then want another column to show the week number, I’m using the following code to populate the array after extracting data from a database:

Code:
$results[$i][0] = $f1->value;
$results[$i][1] = $f2->value;
$results[$i][2] = $f3->value;
$results[$i][3] = date("W", strtotime($f3->value));

However, the final line seems to be getting the month and date transposed. For example, 4th January 2011 (04/01/2011) should be showing as week 1, but it’s coming back as week 13 as its seeing the date as 1st April 2011 (01/04/2011).

I’ve looked around trying to find a way to tell PHP to use dd/mm/yyyy but no joy so far. Anyone able to advise?

Thanks
 
Its a date string in the format dd/mm/yyyy, i.e. 09/09/2011.

This date is definitely in the correct format, if I sort on that column then all dates appear in chronological order. Its something to do with that final line that's seeing the dates in mm/dd/yyyy format.
 
That's because using / as the delimiter makes the strtotime function assumes the American format, the dd-mm-yyyy format is delimited by a -

you may have to either store your dates in a different format or convert the / to - before you process the date.

something like:
Code:
$results[$i][3] = date("W", strtotime(str_replace("/", "-", "$f3->value")));

Thanks, that's sorted it.

Or not rely on such an ambiguous function like strtotime.

http://uk3.php.net/manual/en/datetime.createfromformat.php

use that instead.

Funnily enough I was just looking at that page, cheers anyway.
 
Back
Top Bottom