Page 1 of 1

Dates picking up incorrect format

Posted: Wed Sep 07, 2005 6:01 am
by graz79
Having a problem with dates.

If I do the following calculations

Code: Select all

$i_str_date = '31/10/1979';
echo "<br/>".$i_str_date;
echo "<br/>".strtotime($i_str_date);
echo "<br/>".strftime('%c',strtotime($i_str_date));
echo "<br/>".date('Y-m-d',strtotime($i_str_date));
the output is

Code: Select all

31/10/1979
363567600
10/07/1981 00:00:00
1981-07-10
I have checked all server settings everything shows as English. I have tried adding

Code: Select all

setlocale (LC_TIME, "uk");
but to no avail. It seem to insist on the date being mm/dd/yyyy.

Any suggestions other than parsing the string out and re-ordering it?

Thanks

Posted: Wed Sep 07, 2005 6:56 am
by Chris Corbyn
Yes, that's the way strtotime() is written. There's no two ways around it. Use a U.S. style date (mm/dd/yyyy) or another format it reads (such as yyyymmdd).

You can fairly easily write a function to switch from U.K. to U.S. style dates ;)

Code: Select all

function uk2usdate($date, $sep='/')
{
    $re = '/^(\\d{2})'.preg_quote($sep).'(\\d{2})'.preg_quote($sep).'(\\d{4})$/';
    preg_match($re, $date, $m);
    return $m[2].$sep.$m[1].$sep.$m[3];
}