Menu Close

How to change date format in PHP?

0
(0)

Most of the time we require to change the date format when we store the Date in the database and display products list with date. Also, the date format is important in events showing and product bidding like auction sites. Let’s see how to change the date format with some examples. Here, I have changed d/m/y to y-m-d, m-d-y to y-m-d, d/m/Y to Y-m-d in PHP. Just follow the below code snippets to change date formats in PHP.

1. Change yyyy-mm-dd to mm/dd/yyyy in PHP

Here, we will add “2020-04-18” date with format yyyy-mm-dd into this mm/dd/yyyy format in PHP

<?php

$myOriginalDate = date("Y-m-d"); or $myOriginalDate = date("2020-04-18");
  
$myNewDate = date("m/d/Y", strtotime($myOriginalDate));
   
print_r($myNewDate);

?> 

Output

Output: 04/18/2020

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

Here, we will add “2020-04-18” date with yyyy-mm-dd and change into this dd-mm-yyyy format in PHP

<?php

$myOriginalDate = date("Y-m-d"); or $myOriginalDate = "2020-04-18";
  
$myNewDate = date("d-m-Y", strtotime($myOriginalDate));
   
print_r($myNewDate);

?> 

Output

Output: 18-04-2020

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

Here, we will add “18/04/2020” date with dd/mm/yyyy and change into this yyyy-mm-dd format in PHP

<?php

$myOriginalDate = date("d/m/Y"); or $myOriginalDate = "18/04/2020";
   
$myOriginalDate = str_replace('/', '-', $myOriginalDate );

$myNewDate = date("Y-m-d", strtotime($myOriginalDate));
   
print_r($myNewDate);

?> 

Output

Output: 2020-04-18

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Share

Posted in PHP

Leave a Reply

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

You can also read...