Reputation: 133
I am using date_create to create a date object. I realized when I ran this code
$date=date_create("7 December, 2017");
echo date_format($date,"Y/m/d");
it outputs this:
2018/12/07
The year is wrong. What's the way to fix this?
Upvotes: 2
Views: 325
Reputation: 108
=> Try this code ..
<?php
$date=date_create("7 December 2017");
echo date_format($date,"Y/m/d");
?>
Error in Format
for more info check http://php.net/manual/en/datetime.formats.date.php
Upvotes: 3
Reputation: 512
If you use default date_create() function you must remove the comma after the month. But if you need to parse the input with the comma or any other format you can use date_create_from_format instead.
$date = date_create_from_format('d M, Y', '7 December, 2017');
echo date_format($date,"Y/m/d");
The output will be as expected
2017/12/07
Upvotes: 2