PHP strtotime 只有在添加天数时才能正确转换
PHP strtotime only converting correctly when adding days
我在 PHP 中使用 strtotime,从同一输入得到两个不同的结果。我不知道为什么。
$startDate = 1468467219;
$oldDate = date('d/m/Y', strtotime($startDate));
$newDate = date('d/m/Y', strtotime('+3 weekdays', $startDate));
原来的日期是14/07/2016 01.33 PM
$newDate
返回 19/07/2016
正如预期的那样。
$oldDate
返回 01/01/1970
不是预期的结果 - 应该是 14/07/2016
.
我尝试了 strtotime 中的其他函数,它们都产生了正确的结果。我错过了什么?为什么我不能简单地将 1468467219
传递给 strtotime 而不修改它?
你应该只使用:
$oldDate = date('d/m/Y', $startDate);
所以,没有 strtotime($startDate)
当您使用 strtotime
时,第二个参数应该是时间戳,但在您的情况下它是第一个。但作为第一个参数应该是日期和时间格式之一。
您在滥用 strtotime
。此函数采用日期的字符串表示形式和 returns 时间戳。相反,你给它一个时间戳
$startDate = 1468467219;
$oldDate = date('d/m/Y', strtotime($startDate));
由于没有通用的日期格式表示为"today is 1468467219",函数无法解析它并且returns false。
var_dump(strtotime($startDate)) //<-- boolean FALSE
当您继续将 FALSE
提供给 date
函数时,它也无法解析它,因此 returns 错误的日期:01/01/1970
。
要获得结果,您只需将时间戳直接提供给 date
:
$oldDate = date('d/m/Y', $startDate);
我在 PHP 中使用 strtotime,从同一输入得到两个不同的结果。我不知道为什么。
$startDate = 1468467219;
$oldDate = date('d/m/Y', strtotime($startDate));
$newDate = date('d/m/Y', strtotime('+3 weekdays', $startDate));
原来的日期是14/07/2016 01.33 PM
$newDate
返回 19/07/2016
正如预期的那样。
$oldDate
返回 01/01/1970
不是预期的结果 - 应该是 14/07/2016
.
我尝试了 strtotime 中的其他函数,它们都产生了正确的结果。我错过了什么?为什么我不能简单地将 1468467219
传递给 strtotime 而不修改它?
你应该只使用:
$oldDate = date('d/m/Y', $startDate);
所以,没有 strtotime($startDate)
当您使用 strtotime
时,第二个参数应该是时间戳,但在您的情况下它是第一个。但作为第一个参数应该是日期和时间格式之一。
您在滥用 strtotime
。此函数采用日期的字符串表示形式和 returns 时间戳。相反,你给它一个时间戳
$startDate = 1468467219;
$oldDate = date('d/m/Y', strtotime($startDate));
由于没有通用的日期格式表示为"today is 1468467219",函数无法解析它并且returns false。
var_dump(strtotime($startDate)) //<-- boolean FALSE
当您继续将 FALSE
提供给 date
函数时,它也无法解析它,因此 returns 错误的日期:01/01/1970
。
要获得结果,您只需将时间戳直接提供给 date
:
$oldDate = date('d/m/Y', $startDate);