Dates picking up incorrect format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
graz79
Forum Newbie
Posts: 4
Joined: Thu Aug 18, 2005 9:38 am
Location: Newcastle Upon Tyne, UK

Dates picking up incorrect format

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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];
}
Post Reply