How to Change Date Format in PHP

It is very common that people would change dates in their projects. People want to change the date in different formats.Now i am share to how to change date format in php. You can change the date-time using strtotime () and date() function.

Suppose you have date in YYYY-MM-DD format and you want to change this to MM-DD-YYYY format. Now i have give some example to change date format


$yourDate = "2018-05-31"; 
$newDate = date("d-m-Y", strtotime($yourDate)); 
echo "New date format is: ".$newDate


 New date format is: 31-05-2018

Change date time to another format

The following example will convert the date format MM/DD/YYYY to YYYY-DD-MM format and 12 hours time clock to 24 hours time clock

$yourDate = "05/31/2018 8:25 PM";   
$sec = strtotime($yourDate );  
$newdate = date("Y-d-m H:i:s", $sec);  
$newdate = $newdate;     
echo "New datetime format is: ".$newdate; 


New datetime format is: 2018-31-05 20:25:00

Leave a Reply

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