$ddd=`1 December, 2014`;
$from = date("Y-m-d 00:00:00", strtotime($ddd));
echo $from.
Returns 2015-12-01 00:00:00
instead of 2014-12-01 00:00:00
Is there any solution for this?
In strtottime() documentation, it says:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Therefore, you should convert the $ddd
to an acceptable date format before using strtotime()
. Even better, if you have PHP 5.3+, you can use the DateTime::createFromFormat() function:
$date = DateTime::createFromFormat('!d F, Y', '1 December, 2014');
where $date
is a DateTime
object.
Refer this answer. It works.
$ddd='1 December 2014';
$from = date("Y-m-d 00:00:00", strtotime($ddd));
echo $from;
<?php
$ddd='1 December 2014';
$from = date("Y-m-d 00:00:00", strtotime($ddd));
echo $from;
?>
demo you should remove ',' from the date '1 December 2014'
strtotime only understands a specific set of formats. If your input is not in one of these formats, it will do its best to guess, but as you can see the results can vary.
try this :
<?php
$ddd = '1 December, 2014';
$t = date_create_from_format("d M, Y",$ddd);
$from = date_format($t,"Y-m-d 00:00:00");
echo $from;
?>
'; $from = date("Y-m-d 00:00:00", strtotime($ddd)); echo $from.'
'; } – kparsiya Jan 13 '15 at 06:46