Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date from YYYYMMDD to DD/MM/YYYY format in PHP [duplicate]

Tags:

date

php

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?

like image 706
rahules Avatar asked Dec 26 '12 19:12

rahules


People also ask

How convert date from yyyy-mm-dd to dd-mm-yyyy format in PHP?

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.

How do I change date format from YYYY-mm-DD in PHP?

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));


1 Answers

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

like image 54
jeremy Avatar answered Sep 20 '22 16:09

jeremy