PHP strtotime仅在添加天数时正确转换

时间:2016-07-15 02:42:15

标签: php strtotime

我在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而不进行修改?

2 个答案:

答案 0 :(得分:1)

你应该只使用:

  string myString3="abcdef";
  var items1=Dababase1Repository.Find<MyTableClass1>(x=>string.Concat(x.Str1+x.Str2)==myString3).List();

所以,没有$oldDate = date('d/m/Y', $startDate);

当您使用strtotime($startDate)时,第二个参数应该是时间戳,但在您的情况下,它是第一个参数。但是,第一个参数应该是日期和时间格式之一。

更多信息: http://php.net/manual/en/function.strtotime.php

答案 1 :(得分:0)

您滥用strtotime。此函数采用日期的字符串表示形式,并返回时间戳。而是你给它添加一个时间戳

$startDate = 1468467219;
$oldDate = date('d/m/Y', strtotime($startDate));

由于没有公共日期格式表示为&#34;今天是1468467219&#34;,该函数无法解析它并返回false。

var_dump(strtotime($startDate)) //<-- boolean FALSE

当您继续将FALSE提供给date功能时,它也无法对其进行解析,因此会返回错误的日期:01/01/1970

要获得结果,您只需将时间戳直接提供给date

$oldDate = date('d/m/Y', $startDate);