The date()
function takes as a parameter a number that is a timestamp (that is, the number of seconds that passed since 01/01/1970), so it returns that date.
Modify the string directly
What you have is a string
that is formed as ddmmaaaa
(day-month-year). The only thing you need to give it the format you are looking for is to add the scripts to separate them.
We use substr_replace()
to add a script to the position 2 and in position 4:
echo substr_replace(substr_replace($fechaIn, '-', 4, 0), '-', 2, 0);
Convert a string to date
If you would like to convert the string to date format first, the best option is to use DateTime :: createFromFormat () :
$fechaIn = '31082016';
//Convertir un string a fecha
$fechaDateTime = DateTime::createFromFormat('dmY', $fechaIn);
//Imprimir la fecha en el formato deseado
echo $fechaDateTime->format('d-m-Y');
Result
31-08-2016