I want to convert my string to date, I know that I can use DateTime::createFromFormat
, my problem is that there is no separator in $d
<?php $d = "01122015"; ?>
expected output :
01/12/2015
You can use format method of it to format as per your wish,
$d = "01122015";
$date = DateTime::createFromFormat('dmY', $d);
echo $date->format('d/m/Y');
Output:-
01/12/2015
Also you can achieve it by
$a = '01122015';
$b = '';
$cnt = 0;
$otheCnt= 0;
for($i=0;$i<strlen($a);$i++){
$cnt++;
$b .= $a[$i];
if($cnt==2 && $otheCnt < 2){
$otheCnt++;
$b .= '/';
$cnt = 0;
}
}
echo $b;
Output:
01/12/2015