With a date string of Apr 30, 2010
, how can I parse the string into 2010-04-30
using PHP?
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.
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.
The date_parse() function returns an associative array with detailed information about a specified date.
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"; ?>
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With