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
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
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
Learn about Change Phone Numbers Format In PHPÂ
Share