PHP: Help with this date formatting

混江龙づ霸主 提交于 2019-12-01 10:27:05

The answer is that the European date format uses dashes, not slashes.

According to the manual:

Note:

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.

Using a correct format works like a charm:

// American format
//

$_POST['from_date'] = "02/06/2011";    
$yankeeTimestamp = strtotime($_POST['from_date']);

// European format
//

$_POST['from_date'] = "06-02-2011";    
$euroTimestamp = strtotime($_POST['from_date']);

echo date("m/d/Y", $yankeeTimestamp); // returns 02/06/2011
echo date("m/d/Y", $euroTimestamp); // returns 02/06/2011

What I would do is explode the date by '/' and make a new date with mktime:

$from = explode('/', $_POST['from_date']);
$from = mktime(0, 0, 0, $from[1], $from[0], $from[2]);
$from = mdate($datestring, $from);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!