PHP将字符串转换为日期[重复]

This question already has an answer here:

Hi I am new into PHP and I really really like it so var. I have a string which I want to convert into a custom date and time by adding just simple characters. I first tried fixing it by myself, but everything I tried didn't worked for me.

Input:

$customdate = "27032017042100";

I want to add to the string above characters like: / and :

Expected result:

27/03/2017 04:21:00

</div>

Changing date format from one to another.

PHP code demo

$date = date_create_from_format('dmYHis', '27032017042100');
echo date_format($date, 'd/m/Y H:i:s');

This is a simple date function:

echo date('d/m/Y h:i:s');

See http://php.net/manual/es/function.date.php for more ideas.

Its quite simple.

<?php
    $customdate = "27032017042100";
    echo gmdate("Y-m-d\TH:i:s\Z", $customdate);
?>

Here is the previous answered question: Converting a UNIX Timestamp to Formatted Date String

If you need to convert your data from STRING, you can use folowing code

if (preg_match("/([0-9]{2})([0-9]{2})([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})/", $customdate, $rg))
    $newdate = $rg[1]."/".$rg[2]."/".$rg[3]." ".$rg[4].":".$rg[5].":".$rg[6]