How do I transform, for example, a string "11092018"
into "11-09-2018"
?
Is there a function I can use?
How do I transform, for example, a string "11092018"
into "11-09-2018"
?
Is there a function I can use?
date_parse_from_format
$data = date_parse_from_format('dmY', '11092018');
$data['month'] = str_pad($data['month'], 2, 0, STR_PAD_LEFT);
$data['day'] = str_pad($data['day'], 2, 0, STR_PAD_LEFT);
$formatada = "{$data['day']}-{$data['month']}-{$data['year']}";
See working at IDEONE .
Manual:
DateTime::createFromFormat
$data = DateTime::createFromFormat('dmY', '11092018');
$formatada = $data->format('d-m-Y');
See working at IDEONE .
Manual:
preg_match
if (preg_match('/(\d{2})(\d{2})(\d{4})/', '11092018', $matches)) {
echo "{$matches[1]}-{$matches[2]}-{$matches[3]}";
}
See working at IDEONE .
Manual: