tags:

views:

571

answers:

5

I'm trying to parse a string in a specific format and I'm really surprised to discover that I can't find a good function to do that.

The only one I found is strtotime and it doesn't fit as it guesses the date format. I really don't trust the "guess" part. Moreover my string input is in a french format (dd/mm/aaaa) which it seems that it's not well understood (it parses american formats like mm/dd/aaaa).

What I'm looking for is a function that take in input a date string and a format to parse. I could do it myself with a regexp but I can't believe that it doesn't already exist.

I found :

  • DateTime::createFromFormat(). But it only work with PHP 5.3 and I don't have the power to upgrade the PHP version (5.2)
  • strptime(). This method does what I want but is not implemented on windows platform (by the way: WTF ??)

Any suggestion ?

+4  A: 

Unfortunately, it seems that such parsing is better done manually, by exploding the string at slashes and then switching day and month.

n1313
Yea, it wouldn't be difficult at all to flip the month/date and then run strtotime().
Nerdling
It was just to "not reinvent the wheel". But if I parse it, strtotime is useless, i'd prefer use mktime().
Julien N
Accepted. Because while the other answers are correct, they parse manually AND use strtotime() while it seem quite useless once the string is already parsed.The best solution seems to use regular expressions to parse the string then to use mktime() to get the unix time.
Julien N
+4  A: 

Check out Zend_Date, which lets you specify the format when you set a date. As well as including constants for many common formats, you can specify your own too.

$date = new Zend_Date();
$date->set('27/08/2009','DD/MM/YYYY');
Paul Dixon
+1  A: 

The following comment from php.net on strtotime may help:

Fails for non-US dates where the ordering is uncertain, such as 01/02/2003 - parses this as Feb 1st, rather than Jan 2nd.

If you are parsing dates for a non-US locale, you can flip these elements of your date:

<?php 
$y = $_POST['date']; 
if (preg_match('/^\s*(\d\d?)[^\w](\d\d?)[^\w](\d{1,4}\s*$)/', $y, $match)) { 
  $y = $match[2] . '/' . $match[1] . '/' . $match[3]; 
} 
echo date('d # m # Y', strtotime($y)); 
?>

WARNING: Above only works for dates, and breaks for times: 12:30:01 will be converted to 30/12/01.

Waggers
A: 

If you know your date format input will be English-formatted, then you can process it into a more standard date format. A simple parsing of 24/7/2007 to 2007-07-24 is trivial. Explode with the forward slash and put the parts in the right spot. I know for a fact that strtotime will parse 2007-07-24 correctly.

patcoll
+1  A: 

I've written a class myself, I think you'll find an ok version in gadmdatecommand.php in http://sourceforge.net/projects/phpdbedittk

Regarding the comments here to just explode by '/' and swap the number, its not quite that simple. If you offer to enter dates into an input box, you may get - depending on the locality of the user and the application

1/7/2010 1.7.2010 1-7-2010 15 Jul 1 Jul 2010 1/6/8

and many more variations. I've solved this problem (at least for me successfully) by creating dateformats, each of which have a) a regex that matches the format b) an array mapper that matches regex brackets into date pieces (day, month, minute, am/pm) c) an output format for date()

HTH

jdog