I've a MySQL database table in which the date is stored in YYYYMMDD format. Eg: 20121226.
I want to display this date in DD/MM/YYYY format. Eg: 26/12/2012
What I came up with is to use substr
to extract the day, month and year separately.
I would like to know if there's an easier way to do this.
Also, is there a way to convert this date to "26 December 2012" format without the need to write separate code?
How convert date from yyyy-mm-dd to dd-mm-yyyy format in PHP? Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format.
In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format. $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate));
You can easily use the DateTime class to do this
$retrieved = '20121226';
$date = DateTime::createFromFormat('Ymd', $retrieved);
echo $date->format('d/m/Y');
http://php.net/manual/en/datetime.format.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With