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

Change YYYY-MM-DD to DD-MM-YYYY

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

Output

 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

<?php    
$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; 
?>

Output

New datetime format is: 2018-31-05 20:25:00
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments