天天看点

strtotime("+1 month", $strtimes) 导致时间戳错误

如果要获取当前时间中未来一个自然月的这个linux时间戳时

使用  $nexttime = strtotime("+1 month", $strtimes); 是不精确的,尤其$strtimes无效时。

因为PHP中 “+1 month” 默认是30天,strtotime中 “+1 month”大月31、小月30、2月是28天,因此会导致时间戳错误

要使用

$nexttime =mktime(date("H"),date("i"),date("s"),date("m")+1,date("d"),date("Y"));

如果说时间是2013-02-29(此时间的linux值实现为2013-03-01),而需要逻辑处理后是2013-03-29;所以需要做加一个月处理,万一在处理时间的时候是 strtotime("+1 month",strtotime("2013-02-29"))的方法;得出的时间是:2013-04-01;因此会导致一系列的时间相减为负数。

注:别说 2013-02-29 是不存在,这全世界人民都知道,可有时候程序中会存在的。

实例:

$str1 = strtotime("2013-02-29 14:35:59");

echo (time() - strtotime("+1 month",$str1))/(24*3600);  //为负

$nexttime =mktime(date("H",14),date("i",35),date("s",59),date("m",02)+1,date("d",29),date("Y",2013));

echo (time() - $nexttime)/(24*3600);  //为正