Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Using strtotime with a UK date format (dd-mm-yy)

Quite simply in PHP I have a date of 8th January 2011, in the format 08-01-11 - when I run this into strtotime and convert it back into a different date format, it reverts back to 1st August 2011 - not ideal!

Is there any easy way around this, rather than having to place everything into different arrays/variables and back again?

Thank you!

like image 698
Nick Avatar asked Nov 12 '10 10:11

Nick


People also ask

How can get date in dd mm yyyy format in PHP?

In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format. $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate));

What does Strtotime mean in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How do you get a date from Strtotime?

Code for converting a string to dateTime$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

Why is Strtotime return false?

strtotime expects a "English textual datetime" (according to the manual), which Y-D-M is not. Any time strtotime returns false, it simply doesn't understand your string, which in this application is expected.


1 Answers

The perfect solution would be for the US to use the correct date format in the first place... ;0)

I do this to get around it:

$date = "31/12/2012";
$bits = explode('/',$date);
$date = $bits[1].'/'.$bits[0].'/'.$bits[2];

$date is now strtotimeable

like image 82
myshadowself Avatar answered Sep 19 '22 19:09

myshadowself