日期格式无法正常工作
Date format not working correctly
我正在提交一个包含日期范围选择器的表单。
我有从表单中获取选择器值的代码,格式如下:
01/03/2017 - 01/03/2017
我也有它,所以它将日期范围分成两个变量。
$one = 20/01/2017
$two = 13/03/2017
我现在正在尝试将这些变量的日期格式化为 MYSQL 使用的格式。
我的问题是结束日期会失败并一直显示为 1970-01-01。
// Get the range from the form
$daterange = $_POST["daterange"];
// Split the range into a start and end date
list($one, $two) = explode("-", "$daterange", 2);
// Test the split worked
echo $one;
echo $two;
// Format the start date into MySQL style
$StartDate = date("Y-m-d", strtotime($one));
// Test date
echo $StartDate;
// Format the end date into MySQL style
$EndDate = date("Y-m-d", strtotime($two));
//Test date
echo $EndDate;
试试这个:
$one = '20/01/2017';
$one = str_replace('/', '-', $one);
echo date('Y-m-d', strtotime($one));
// 输出:2017-01-20
For the reason, see The PHP Manual for strtotime()
Specifically this note
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. 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.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
用这个来格式化你的日期:
$StartDate = date_format(date_create_from_format('d/m/Y', $one), 'Y-m-d');
反转:
$reverse = date('d/m/Y', strtotime($StartDate));
就我个人而言,我更喜欢不预设任何东西的date_create_from_format
。
一行:
$StartDate = date_create_from_format( "d/m/Y" , $one )->format("Y-m-d");
我正在提交一个包含日期范围选择器的表单。
我有从表单中获取选择器值的代码,格式如下:
01/03/2017 - 01/03/2017
我也有它,所以它将日期范围分成两个变量。
$one = 20/01/2017
$two = 13/03/2017
我现在正在尝试将这些变量的日期格式化为 MYSQL 使用的格式。
我的问题是结束日期会失败并一直显示为 1970-01-01。
// Get the range from the form
$daterange = $_POST["daterange"];
// Split the range into a start and end date
list($one, $two) = explode("-", "$daterange", 2);
// Test the split worked
echo $one;
echo $two;
// Format the start date into MySQL style
$StartDate = date("Y-m-d", strtotime($one));
// Test date
echo $StartDate;
// Format the end date into MySQL style
$EndDate = date("Y-m-d", strtotime($two));
//Test date
echo $EndDate;
试试这个:
$one = '20/01/2017';
$one = str_replace('/', '-', $one);
echo date('Y-m-d', strtotime($one));
// 输出:2017-01-20
For the reason, see The PHP Manual for
strtotime()
Specifically this noteNote: 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.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
用这个来格式化你的日期:
$StartDate = date_format(date_create_from_format('d/m/Y', $one), 'Y-m-d');
反转:
$reverse = date('d/m/Y', strtotime($StartDate));
就我个人而言,我更喜欢不预设任何东西的date_create_from_format
。
一行:
$StartDate = date_create_from_format( "d/m/Y" , $one )->format("Y-m-d");