Format date from yyyymmdd to Y-m-d with PHP

date, php

Solution

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

Problem

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

Original source

Related problems