1

In a database the date is stored like this 20171127 which is todays date. Unfortunately I cannot change the way the date is stored in the database. Can I convert this string to a nice format? The excepted output is 27. November 2017.

I already looked at strtotime and strftime but there is no way to parse a string like I got.

Fabian
  • 541
  • 9
  • 30
  • 1
    Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – iainn Nov 25 '17 at 10:47
  • Not sure what you've tried, but `strtotime` should work fine with that format, see https://eval.in/907457 – iainn Nov 25 '17 at 10:50

2 Answers2

2

You can do it, use date() function and combine with strtotime()

date('d F Y', strtotime($yourdatehere));

//Example
<?php echo date('d F Y',strtotime('20171112')); ?>

The result will print 12 November 2017 & 100% tested & worked on my system.

Zinc
  • 385
  • 1
  • 5
  • 19
0

try this:

$str = "20171127";
$date = DateTime::createFromFormat('Ymd', $str);

take a look here for the specifics: datetime php manual

musashii
  • 445
  • 6
  • 13