How to convert date format in PHP?

May 8, 2018 | Category : PHP

Hi Guys,

Today, i will let you know how to change date format like Y-m-d to d-m-Y or yyyy-mm-dd to dd-mm-yyyy etc as you want to change. So no worry here you will see simple and easy example for php developer.

Actually, i was working on my old core php project and i need to change date format Y-m-d to d-m-Y. we always use date picker and we need to convert date formate for save data in mysql database. reason is mysql date accept y-m-d So. Here same issue i have i require to change date format using PHP.

Here we will use strtotime() php date function and date(). strtotime() is a amazing php date function. strtotime() will help to convert date formate in php. So basically looks bellow example, how it simple and it will help you a lot.

PHP Example:

<?php


$oldDate = "2018-05-22";

$changedDate = date("d-m-Y", strtotime($oldDate));


print_r($changedDate);


?>

I hope you found your best solution....