PHP | Convert date from one format to another in php

|
| By Webner

To convert date from one format to another we need to specify to php interpreter the format of our current value. See code below:

/*
In statement below, we are providing date and format to DateTime::createFromFormat function so that function could know day, month and year values separately
*/

$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
//now we can convert to any format we want
echo $date->format('Y-m-d');

There is another way to achieve the same:

$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');

Both will output the date as 2009-02-15 .

date_create_from_format function is actually an alias of: DateTime::createFromFormat() so they both are the same.

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

Leave a Reply

Your email address will not be published. Required fields are marked *