-1

My current Date format is in MM/YY. I Need to default days to my format in php.

For example:

12/2009 -> 07/12/2009

I tried this code:

$currdate = '07/'.$currdate;
$newFormat = date('d-M-Y',strtotime($currdate));

But the new format is wrong, it output 12/07/2009.

----------------- Edit -----------------------------

I have tried **DateTime::createFromFormat**.Since my $currdate date format has only month and year its not accepting.I am getting a fatal error.

1 Answers1

4

strtotime expects an american date format. Use datetime::createFromFormat instead:

$date = DateTime::createFromFormat('d/m/Y', $currdate);
echo $date->format('Y-m-d');

Edited to better explanation:

When you use date with slashes(/), PHP strtotime will think it is in m/d/Y format, the american way. If you use dash (-) it will assume d-m-Y format. If you use dot (.) it will assume Y.m.d format.

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29