0
$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?

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
kparsiya
  • 7
  • 1
  • 3
    backtick instead of quotes & a `.` after `echo` line? that's syntax error, isn't it? – Raptor Jan 13 '15 at 06:41
  • try changing `$from.` to `$from;` – Ronser Jan 13 '15 at 06:43
  • Also, the date does not fall into any valid date format accepted by `strtotime()`: http://php.net/manual/en/datetime.formats.date.php , thus PHP blind guesses it. – Raptor Jan 13 '15 at 06:44
  • $from="0"; if (isset($_POST['input_01'])) /* for example 1 December, 2014 */ { $ddd=$_POST['input_01']; echo $ddd.'
    '; $from = date("Y-m-d 00:00:00", strtotime($ddd)); echo $from.'
    '; }
    – kparsiya Jan 13 '15 at 06:46
  • Don't use punctuation mark, http://codepad.org/BCwn82qP – egig Jan 13 '15 at 06:46
  • // please remove the comma // $ddd='1 December 2014'; $from = date("Y-m-d", strtotime($ddd)); echo $from; – Arun Jan 13 '15 at 06:47
  • possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Glavić Jan 13 '15 at 07:41

4 Answers4

2

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.

Raptor
  • 53,206
  • 45
  • 230
  • 366
1

Refer this answer. It works.

 $ddd='1 December 2014';
    $from = date("Y-m-d 00:00:00", strtotime($ddd));
    echo $from;
Ajitha Ms
  • 545
  • 5
  • 18
0
<?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'

Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42
0

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;

 ?>
Anand Patel
  • 3,885
  • 3
  • 17
  • 23