---
title: "strtotime(\"+1 month\", $strtimes) 导致时间戳错误"
author: "yd514"
date: "2013-03-29T16:06:26Z"
tags: ["strtotime"]
categories: ["编程语言"]
slug: "strtotime-1-month-strtimes"
draft: false
lastmod: "2013-03-29T18:01:47Z"
id: 1838818
url: "https://yd514.iteye.com/blog/1838818"
---

如果要获取当前时间中**未来一个自然月**的这个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);  //为正