MySQL DateTime format is Y-m-d H:i:s known as Year-month-day hour:minute:second. It is the bare bones datetime format for both MySQL and PHP…. it contains all you need to know and can be shaped or formatted from this.
Displaying the dateTime as: 2019-04-24 11:47:47 isn’t pretty nor may it be needed, sometimes you just need the date and not time or vice versa. Here is how to format dateTime in PHP.
Calling the dateTime through a simple MySQL SELECT statement:
$result = mysqli_query($connect, "SELECT `date_time` FROM `table` LIMIT 1"); $ar = $result->fetch_assoc(); $db_date = $ar['date_time']; echo $db_date;//2019-04-24 11:47:47
This outputs 2019-04-24 11:47:47
As $db_date is actually a string rather than the PHP DateTime object format you cannot format it using date_format() yet. It will throw an error such as: Warning: date_format() expects parameter 1 to be DateTime, string given in
Fix this by creating a new DateTime object with createFromFormat :
$myDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $db_date);//string to datetime
Now you can format your dateTime as you please, check here for the format types. Provided are some examples:
$format_date = $myDateTime->format('d-m-Y');//24-04-2019 $format_date = $myDateTime->format('D jS F Y');//Wed 24th April 2019 $format_date = $myDateTime->format('g:ia D jS F Y');//11:47am Wed 24th April 2019 $format_date = $myDateTime->format('g:ia l j/m Y');//11:47am Wednesday 24/04 2019 $format_date = $myDateTime->format('d/m/y');//24/04/19 $format_date = $myDateTime->format('jS F Y');//24th April 2019 $format_date = $myDateTime->format('g:ia l jS F Y');//11:47am Wednesday 24th April 2019 $format_date = $myDateTime->format('ga D jS M Y');//11am Wed 24th Apr 2019 $format_date = $myDateTime->format('g:ia');//11:47am $format_date = $myDateTime->format('G:i d/m/y');//11:47 24/04/19
A drained and empty Kennington reservoir images from a drone in early July 2024. The…
Merrimu Reservoir from drone. Click images to view larger.
Using FTP and PHP to get an array of file details such as size and…
Creating and using Laravel form requests to create cleaner code, separation and reusability for your…
Improving the default Laravel login and register views in such a simple manner but making…
Laravel validation for checking if a field value exists in the database. The validation rule…