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
Popular Posts
- Show / Hide div based on dropdown selected using jQuery
- Autosuggestion Select Box using HTML5 Datalist, PHP and MySQL with Example
- Custom Authentication Login And Registration Using Laravel 8
- Infinite Scrolling on PHP website using jQuery and Ajax with example
- Google Login or Sign In with Angular Application
- How to Convert MySQL Data to JSON using PHP
- How to change date format in PHP?
- Image Lazy loading Using Slick Slider
- Slick Slider Basic With Example
- php in_array check for multiple values
- Adaptive Height In Slick Slider
- Slick Slider Center Mode With Example