PHP strtotime / date_create error conversion from "j M, Y" to "Ymd"

I have an error with strtotime and date_create. In my code, the date conversion from "07 Sep, 2014" to "2015-09-07" is incorrect. Please note that the year is wrong!

Here is my code:

<?php

$listing_date = "07 Sep, 2014";
print $listing_date . " --> ";
$listing_date = date_create($listing_date)->format('Y-m-d');
print $listing_date . "\n";

$listing_date = "07 Sep, 2014";
print $listing_date . " --> ";
$listing_date = date('Y-m-d', strtotime($listing_date));
print $listing_date . "\n";

?>

... and here is the result:

07 Sep, 2014 --> 2015-09-07
07 Sep, 2014 --> 2015-09-07

Am I doing something wrong? Council appreciated.

+4
source share
1 answer

you must remove commafrom $listing_dateor change the new format to 'Y-m,d'Also this situation you should use DateTime :: createFromFormat () to achieve reliable results.

+5
source

Source: https://habr.com/ru/post/1613098/


All Articles