Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtotime With Different Languages?

Does strtotime only work in the default language on the server? The below code should resolve to august 11, 2005, however it uses the french "aout" instead of the english "aug".

Any ideas how to handle this?

<?php     $date = strtotime('11 aout 05');     echo date('d M Y',$date); ?> 
like image 881
Cory Dee Avatar asked Aug 08 '11 21:08

Cory Dee


2 Answers

French month dates are:

janvier février mars avril mai juin juillet août septembre octobre novembre décembre

Hence, for the very specific case where months are in French you could use

function myStrtotime($date_string) { return strtotime(strtr(strtolower($date_string), array('janvier'=>'jan','février'=>'feb','mars'=>'march','avril'=>'apr','mai'=>'may','juin'=>'jun','juillet'=>'jul','août'=>'aug','septembre'=>'sep','octobre'=>'oct','novembre'=>'nov','décembre'=>'dec'))); } 

The function anyway does not break if you pass $date_string in English, because it won't do any substitution.

like image 195
Marco Demaio Avatar answered Sep 22 '22 07:09

Marco Demaio


From the docs

Parse about any English textual datetime description into a Unix timestamp

Edit: Six years down the road now, and what was meant to be a side-note about why strtotime() was an inappropriate solution for the issue at hand became the accepted answer 😲

To better answer the actual question I want to echo Marc B's answer: despite the downvotes, date_create_from_format, paired with a custom Month interpreter will provide the most reliable solution

However it appears that there is still no silver-bullet for international date parsing built-in to PHP for the time being.

like image 38
Mr Griever Avatar answered Sep 21 '22 07:09

Mr Griever