Alex Chen
Alex Chen

Reputation: 133

php date_create showing incorrect year

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

Answers (2)

ASHISH SHARMA
ASHISH SHARMA

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

Vũ Nhật Anh
Vũ Nhật Anh

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

Related Questions