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.

  1. $date=’12/31/2006’;
  2. 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 roundtrip function. Your European dates can get converted back to Yankee days as well.


5 Responses to “Convert American date format to European and vice versa”  

  1. 1 Amadeke

    don’t seem to work as php script

  2. 2 Scott

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

  3. 3 Ben

    Awesome! Although I used $3-$1-$2 (instead of the \\ notation)

  1. 1 PHP - Convert American to European Date Format at GraysUnderground.com
  2. 2 The Life of Matt Campos » Blog Archive » PHP - Convert American to European Date Format


Leave a Reply