how to change date format in PHP

In this article, i would you like to share to with you how do i change date format YYYY-MM-DD to DD/MM/YYYY, YYYY-MM-DD to MM-DD-YYYY , Y-m-d to d-Y-m in php. we will change to date format using date() and strtotime() of php.

I will give you very simple example that helps you to simply change date formate in php. we most of need to change date format when you store date into database and when you display on list page. so i will give you bellow three example that will help you to easily convert date format in php.

So, let’s see three example, so you can use in your core php application.

  1. Convert yyyy-mm-dd to mm/dd/yyyy in PHP
$myTodayDate = "2021-12-27";
  
$myConvertDate = date("m/d/Y", strtotime($myTodayDate));
   
print_r($myConvertDate);

Output

12/27/2021

2. Convert yyyy-mm-dd to dd-mm-yyyy in PHP

$myTodayDate = "2021-12-27";
  
$myConvertDate = date("d-m-Y", strtotime($myTodayDate));
   
print_r($myConvertDate);

Output:

27-12-2021

3. Convert dd/mm/yyyy to yyyy-mm-dd in PHP

$myTodayDate = "27/12/2021";
   
$myTodayDate = str_replace('/', '-', $myTodayDate );
$myConvertDate = date("Y-m-d", strtotime($myTodayDate));
   
print_r($myConvertDate);

Output:

2021-12-27

Now you can check this step by step anyone, it will help you.

I hope it can help you…

i will back with convert date foramt in mysql…..Comming soon

Leave a Comment