Convert American date format to European and vice versa

This 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.

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.

Author: Ted

Accessibility is more than making sure images have alternate text. I work with engineers, product managers, and designers to understand how accessibility impacts the users, set realistic deadlines, and create the solutions to provide a delightful experience to all users, regardless of their physical, sensory, or cognitive ability.

13 thoughts on “Convert American date format to European and vice versa”

  1. > 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);

  2. Agreed, this example simply doesn’t work unless there is something about the PHP environment that needs setting which has not been given here?

  3. 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.

  4. Here’s a date function that works for me.
    European format to American

    function e2aDate($strDate) {
    list($d, $m, $y) = explode(‘/’, $strDate);
    $mk = mktime(0,0,0,$m, $d, $y);
    $americanDate =strftime(‘%Y-%m-%d’,$mk);

    return $americanDate;
    $aDate=e2aDate(“10/02/2011”); //call the function pass date
    $aDate would be set to 2011-02-10

    American to European

    function a2eDate($strDate) {
    list($y, $m, $d) = explode(‘-‘, $strDate);
    $mk = mktime(0,0,0,$m, $d, $y);
    $europeanDate =strftime(‘%d/%m/%Y’,$mk);

    return $europeanDate;
    $aDate=a2eDate(“02/10/2011”); //call the function pass date
    $aDate would be set to 10/02/2011

    change the strftime(‘%d/%m/%Y’,$mk); %d %m %y positions to give correct format.

  5. Great script! Worked perfect without modification, which is rare. For everyone else who thought this script didn’t work, you’re simply wrong. Perhaps you didn’t use it correctly. Worked awesome for my purpose as I couldn’t use they typical date() and strtotime() functions because of the input I was getting. Nice job.

Leave a Reply

Your email address will not be published. Required fields are marked *