I am storing date time in database as $date=date("Y-m-d H:i:s");
Now on most places I use it in the same format as it is stored 2016-03-01 19:04:18, but on one place I would rather prefer output to be March 01 at 19:04 how can I change format of output from database from 2016-03-01 19:04:18 to March 01 at 19:04.
First of all we will need to retrieve the date from the database. You can do this however you please.
What we are going to do next is to pass your date to a PHP DateTime
object. This will give us easy options to modify the format to our liking.
// $dbDate is the date you got from your database
$date = new DateTime($dbDate);
// If we want to make sure PHP uses the correct date format,
// We can also use createFromFormat
$date = DateTime::createFromFormat('Y-m-d H:i:s', $dbDate);
Now that we have this object we are going to output it in the format you want it to be.
echo $date->format('M d').' at '.$date->format('H:i');
// M - Textual representation of the month ex. January, February, March
// d - Day of month in numeric value ex 1, 14, 27
// H - 24-hours representation of the hour ex 17, 23, 08
// i - Minutes ex 23, 45, 58
For more parameters and information:
PHP.net - DateTime()
Format it like this
date('d-M-Y H:i');
instead of
date("Y-m-d H:i:s");
Play around with these d-M-Y H:i
to get the desired result
parse the date variable to it to tell it which variable to format
date("Y-m-d H:i:s", $date);
also see documentation here on date function in PHP