Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : Better date parser than strtotime

Tags:

php

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 ?

like image 749
Julien N Avatar asked Dec 29 '22 16:12

Julien N


1 Answers

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

like image 162
n1313 Avatar answered Jan 09 '23 02:01

n1313