PHP: better date parser than strtotime

I am trying to parse a string in a specific format, and I am very surprised to find that I cannot find a good function for this.

The only thing I found is strtotime , and it is not suitable as it guesses the date format. I really don't trust the "guesswork" part. In addition, my string input is in French format (dd / mm / aaaa), which he does not quite understand (he parses American formats such as mm / dd / aaaa).

I am looking for a function that introduces a date string and format for parsing. I could do it myself with a regex, but I can't believe it doesn't exist yet.

I found:

  • DateTime :: createFromFormat () . But it only works with PHP 5.3, and I have no way to update the version of PHP (5.2)
  • strptime () . This method does what I want, but is not implemented on the Windows platform (by the way: WTF ??)

Any suggestion?

+4
source share
6 answers

Unfortunately, it seems that such a parsing is best done manually , breaking the line with a slash, and then switching the day and month.

+4
source

Check Zend_Date , which allows you to specify the format when setting the date. Besides the constants for many common formats , you can specify your own .

$date = new Zend_Date(); $date->set('27/08/2009','DD/MM/YYYY'); 
+4
source

The following php.net comment on strtotime may help:

Failure for non-US dates when the order of uncertainty, for example 01/02/2003 - analyzes this as February 1, and not January 2.

If you parse dates for non-American locale, you can flip these date elements:

 <?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 all, it works only for dates, and breaks times: 12:30:01 will be converted to 30/12/01.

+1
source

I wrote a class myself, I think you will find the ok version in gadmdatecommand.php at http://sourceforge.net/projects/phpdbedittk

Regarding the comments here, just explode with '/' and replace the number, it's not that simple. If you offer to enter dates in the input field, you can get - depending on the location of the user and application

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

and many other options. I solved this problem (at least for me successfully) by creating a date format, each of which has a) a regular expression that matches the format b) an array display unit that matches the regular expression brackets in the date shapes (day, month, minute, am / pm) c) output format for date ()

NTN

+1
source

If you know that your date input format will be formatted in English, you can convert it to a more standard date format. A simple analysis from 24/7/2007 to 2007-07-24 is trivial. Blast it with a slash and place the parts in the right place. I know strtotime correctly 2007-07-24 .

0
source

strptime():
Internally, this function calls the strptime () function provided by the C system library. This function can exhibit ( ! ) Noticeably different behavior on different operating systems. Using date_parse_from_format() , which does not suffer from these problems, is recommended in PHP 5.3.0 and later .
http://www.php.net/manual/en/function.strptime.php

0
source

All Articles