php 使用 strtotime 转换时出现日期问题,但如何正确执行此操作?
php date issue when converting using strtotime, but how to do this properly?
使用以下代码尝试获取 'Y-m-d',应该 return 2016-05-10,但它是 returning 2016-10-05。
// m-d-Y (Month-Day-Year)
$test_date = '05-10-2016';
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date;
如何获得 'Y-m-d' returned?不尝试使用 explode('-', $test_date)
。使用适当的时间函数可以做到这一点吗?
是的,为此使用 DateTime
对象:
$test_date = '05-10-2016';
$DateTime = DateTime::createFromFormat('m-d-Y', $test_date, new DateTimeZone('utc'));
var_dump($DateTime);
输出
object(DateTime)[8]
public 'date' => string '2016-05-10 15:08:53.000000' (length=26)
public 'timezone_type' => int 2
public 'timezone' => string 'UTC' (length=3)
所以
echo $DateTime->format('Y-m-d'); //2016-05-10
strtotime
如果它们被 -
分隔,则假定欧洲格式的日期,如果它们被 /
分隔,则假定美国日期格式
Note: from the manual strtotime()
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.
所以你可以 str_replace
-
for /
// m-d-Y (Month-Day-Year)
$test_date = '05-10-2016';
$test_date = str_replace('-', '/', $test_date);
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date;
或者最好还是使用 DateTime object
Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.
来源: w3schools
您必须将“-”转换为“/”。
<?php// m-d-Y (Month-Day-Year)
$test_date = str_replace('-', '/', '05-10-2016');
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date; // 2016-05-10
使用以下代码尝试获取 'Y-m-d',应该 return 2016-05-10,但它是 returning 2016-10-05。
// m-d-Y (Month-Day-Year)
$test_date = '05-10-2016';
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date;
如何获得 'Y-m-d' returned?不尝试使用 explode('-', $test_date)
。使用适当的时间函数可以做到这一点吗?
是的,为此使用 DateTime
对象:
$test_date = '05-10-2016';
$DateTime = DateTime::createFromFormat('m-d-Y', $test_date, new DateTimeZone('utc'));
var_dump($DateTime);
输出
object(DateTime)[8]
public 'date' => string '2016-05-10 15:08:53.000000' (length=26)
public 'timezone_type' => int 2
public 'timezone' => string 'UTC' (length=3)
所以
echo $DateTime->format('Y-m-d'); //2016-05-10
strtotime
如果它们被 -
分隔,则假定欧洲格式的日期,如果它们被 /
Note: from the manual strtotime()
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.
所以你可以 str_replace
-
for /
// m-d-Y (Month-Day-Year)
$test_date = '05-10-2016';
$test_date = str_replace('-', '/', $test_date);
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date;
或者最好还是使用 DateTime object
Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.
来源: w3schools
您必须将“-”转换为“/”。
<?php// m-d-Y (Month-Day-Year)
$test_date = str_replace('-', '/', '05-10-2016');
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date; // 2016-05-10