PHP日期函数在某些条件下无法正常工作

I find a problem related to the date function in php

i want to convert a date '04-08-2016'(mm-dd-yyyy) into a different format '2016-04-08'(yyyy-mm-dd). But it produces the result as '2016-08-04'(yyyy-dd-mm) instead of '2016-04-08'(yyyy-mm-dd).

my code is

     $date = '04-08-2016';
     echo date('Y-m-d',strtotime($date));

If i place '/' in place of '-' then it is working fine.

Can anyone tell me why this is happening?

You can use the DateTime object:

$date = '04-08-2016';
$d = DateTime::createFromFormat("m-d-Y", $date);
echo $d->format("Y-m-d");

The reason you need to do this is date conventions. As specified in http://php.net/manual/en/function.strtotime.php

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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

Try this:

$date = '04-08-2016';
$timeArray = strptime($date, '%m-%d-%Y');
$timestamp = mktime(0, 0, 0, $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900);
echo date('Y-m-d', $timestamp);

This allows you to specify the format yourself.