Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a date string in PHP?

Tags:

With a date string of Apr 30, 2010, how can I parse the string into 2010-04-30 using PHP?

like image 820
user295515 Avatar asked May 04 '10 17:05

user295515


People also ask

What is PHP date parse?

PHP | date_parse() Function Return Value: Returns an associative array containing information about the parsed date. Errors/Exceptions: In case if the date format has an error, an error message will appear. Below programs illustrate the date_parse() function.

How does Strtotime work 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.

What is Date_parse?

The date_parse() function returns an associative array with detailed information about a specified date.

How can I compare two dates in PHP?

we can analyze the dates by simple comparison operator if the given dates are in a similar format. <? php $date1 = "2018-11-24"; $date2 = "2019-03-26"; if ($date1 > $date2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?>


1 Answers

Either with the DateTime API (requires PHP 5.3+):

$dateTime = DateTime::createFromFormat('F d, Y', 'Apr 30, 2010'); echo $dateTime->format('Y-m-d'); 

or the same in procedural style (requires PHP 5.3+):

$dateTime = date_create_from_format('F d, Y', 'Apr 30, 2010'); echo date_format($dateTime, 'Y-m-d'); 

or classic (requires PHP4+):

$dateTime = strtotime('Apr 30, 2010'); echo date('Y-m-d', $dateTime); 
like image 154
Gordon Avatar answered Sep 23 '22 12:09

Gordon