Date play an important role in any project. We have to change date format according to our need. If we are using PHP 5.2 and lower then for changing date format we have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp or we will use strtotime and then change the format like this.
$result=date('Y-m-d H:i:s',strtotime($dateValue));
But for PHP 5.3 and higher version we will use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.
Example:
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
This is the Object oriented style to convert one date format into another in PHP.
0 Comment(s)