6 years ago
Zaid Bin Khalid
36,893 Views
6
In this tutorial, I am going to show you how can you change date format in PHP. You can use PHP default functions to change the date format. I am going to use strtotime() and date() together to change the date format. let’s begin.
strtotime ( string $time [, int $now = time() ] ) : int
date ( string $format [, int $timestamp = time() ] ) : string
Change YYYY-MM-DD to MM-DD-YYYY
Mysql default date format is Year-Month-Date that is why every PHP developer has with a problem.
<?php
$date = "2019-02-08";
$newDate1 = date("m-d-Y", strtotime($date));
$newDate2 = date("l M, d, Y", strtotime($date));
?>
The output will be. You can change the format as per your need for that you need to change the format character listed below.
echo $newDate1;
08-02-2019 //Output
echo $newDate2;
Friday Feb, 08, 2019 //Output
You can change date format with the help of explode() see below example. But this is not a good way to change the date format.
$date = '2019-02-08';
$arr = explode('-',$date);
$newDate = $arr[2].'-'.$arr[1].'-'.$arr[0];
The output will be.
echo $newDate;
08-02-2019 //Output
6 years ago
Zaid Bin Khalid
36,893 Views
6