How to change date format in PHP?

This Article helps to you date format in php using strtotime() as well as date() methods to convert date time formats for user friendly. For simple example you have stored a date like as a YYYY-MM-DD default formats in a php variable as well as required to update this to MM-DD-YYYY date formats.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

How do I Convert Date Format in PHP

I can achive this by converting date first to seconds using strtotime() methods. After that reconstruct date to any formats using date() methods. following is some more examples of conversion:

Everything You Need to Know About Date format in PHP
Everything You Need to Know About Date format in PHP

1. Change YYYY-MM-DD => MM-DD-YYYY

Here i have date yyyy-mm-dd (“2021-04-25”) formats as well as new fresh converting it to mm-dd-yyyy (“04-25-2021”) date formats.

$startDt = "2021-04-25";
 
$resultDt = date("m-d-Y", strtotime($startDt));
echo $resultDt;

Output:

04-25-2021

Also Read:

2. Change YYYY-MM-DD => DD-MM-YYYY

and then i have date yyyy-mm-dd (“2021-04-25”) date formats as well as converting it to dd-mm-yyyy (“25-04-2021”) date formats.

$startDt = "2021-04-25";
 
$resultDt = date("d-m-Y", strtotime($startDt));
echo $resultDt;

Output:

25-04-2021

3. Change DD/MM/YYYY => YYYY-MM-DD

If you have slashes in date formats such as a “25/04/2021” and required to convert / with hyphens (-). The bellow simple example will help you to convert DD/MM/YYYY (“25/04/2021”) to YYYY-MM-DD (2021-04-25).

$startDt = "25/04/2021";
 
$date = str_replace('/', '-', $startDt );
$resultDt = date("Y-m-d", strtotime($date));
echo $resultDt;

Output:

2021-04-25

I hope you get an idea about date format in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Date and Time Formatting Functions
  2.   How to Get Current Date and Time in PHP?
  3.   Difference between two dates in years, months, days in PHP
  4.   PHP change Laravel Date Formats Example
  5.   jQuery Ajax File Upload images using PHP MySQLi
  6.   PHP Multidimensional Array Check if specific array key
  7.   Soft Delete Cascade using Laravel Eloquent Example
  8.   Laravel Carbon format Live datetime Example
  9.   Create Thumbnail And Upload image using PHP
  10.   jQuery Ajax QR Code Generator PHP script Download

Leave a Reply

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