How do I Convert Date Format in PHP

Hello friends,

We uses PHP strtotime() and date() functions to convert date format in php. For example we have stored a date YYYY-MM-DD format in a variable and need to change this to MM-DD-YYYY format.

We can achieve this by converting date first to seconds using strtotime() function. After that reconstruct date to any format using date() function. Examples are below

1. Change YYYY-MM-DD => MM-DD-YYYY
Here we have date yyyy-mm-dd (“2017-03-18”) format and converting it to mm-dd-yyyy (“03-18-2017”) format.

$origDate = “2017-03-18”;
$newDate = date(“m-d-Y”, strtotime($origDate));
echo $newDate;

Output:
03-18-2017

2. Change YYYY-MM-DD => DD-MM-YYYY
Here we have date yyyy-mm-dd (“2018-06-20”) format and converting it to dd-mm-yyyy (“20-06-2018”) format.

$origDate = “2018-06-20”;

$newDate = date(“d-m-Y”, strtotime($origDate));
echo $newDate;

Output:
20-06-2018

 

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

− 2 = 4

%d bloggers like this: