0

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
prc
  • 187
  • 2
  • 14

2 Answers2

1

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
Rahul
  • 18,271
  • 7
  • 41
  • 60
  • Of course, quite, I didn't know that we could not put separator in `createFromFormat('dmY'...`. Thanks I accept the answer in few minutes – prc Oct 17 '19 at 09:42
1

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
Vibha Chosla
  • 733
  • 5
  • 12