Convert American date format to European and vice versa
9 Comments Published 3 years, 1 month ago in PHP Digg ThisThis PHP script came up in a discussion I had today with Jon Whitlock, who’s from the other side of the pond (U.K.) It’s a nifty little regular expression that converts an American date (12/31/2006) into a European date (31/12/2006). It struck me as a neat little snippet that others could probably use.
Updated Code
As Scott mentioned in the comments, I have changed the snippet to put quotation marks around the second regular expression.
$date=“12/31/2006″;
print ereg_replace(“([0-9]+)/([0-9]+)/([0-9]+)”,“\2/\1/\3“,$date);
As he described it, the first section of the ereg_replace statement takes the date and places it into three variables, 1,2,and 3. It then places those variables into a new string in the 2/1/3 pattern. That makes this a round trip function. Your European dates can get converted back to Yankee days as well.

9 Responses to “Convert American date format to European and vice versa”
- 1 Pingback on Oct 16th, 2007 at 6:38 am
- 2 Pingback on Oct 17th, 2007 at 9:31 am
don’t seem to work as php script
> print ereg_replace(“([0-9] )/([0-9] )/([0-9] )”,\\2/\\1/\\3,$date);
should be: print ereg_replace(”([0-9] )/([0-9] )/([0-9] )”,”\\2/\\1/\\3″,$date);
Awesome! Although I used $3-$1-$2 (instead of the \\ notation)
Saved the above into a .php file and ran it, does nothing except output the date as it was.
useless.
Agreed, this example simply doesn’t work unless there is something about the PHP environment that needs setting which has not been given here?
Much simpler and more consistent results by using echo date( ‘m-d-Y’, strtotime( $date) )
I originally posted this script as a reminder. I wanted to go back and look at it if needed in the future. At the time, I didn’t know much about php or regex. So I’m sorry if it doesn’t work.
I would agree with Justin. If I were to do it today, i’d probably convert to unix time stamp and then use the built in date formatting options in PHP. But that is because I dislike regex and the date formatting in PHP is much easier to understand.
However, the point of this snippet is to change from US dates to EU and vice versa. Using the built in php date formatting is only one way. You’d have to localize it for each region.