3

This question shows how to do it with SQL (SQL date format conversion from INT(yyyymmdd) type to date(mm/dd/yyyy)), how can I do it with PHP?

I want to convert dates from something like 20141127 to 2014-11-27 and I don't knwo if there is a build in function, or any usage of the php dates function to achieve this.

Thank you

Community
  • 1
  • 1
JuanBonnett
  • 776
  • 3
  • 8
  • 26

1 Answers1

10

Use strtotime() to convert a string containing a date into a Unix timestamp:

<?php
// both lines output 813470400
echo strtotime("19951012"), "\n",
     strtotime("12 October 1995");
?>

You can pass the result as the second parameter to date() to reformat the date yourself:

<?php
// prints 1995 Oct 12
echo date("Y-m-d", strtotime("19951012"));
?>
mmvsbg
  • 3,570
  • 17
  • 52
  • 73