如何获得Unix时间戳为一个月在未来给定的日期


How to get the Unix Timestamp for one month in the future given a date

假设我从数据库中提取一个字段,一个时间戳字段。

如何从时间戳字段获得Unix时间戳,未来1个月?

我的大脑有点累了,所以我需要再慢跑一下。

$newDate = strtotime('+1 month',$startDate); 

使用strtotime()可以传递'+1 month'来添加一个上下文敏感的月份。

我会使用 strtotime() 函数,将+1 month添加到您的时间戳:

$future = strtotime('+1 month', $current_time);


有了strtotime(),你就不必去想"有些月平均有30天,有些月有31天",或者"二月有时有28天,有时有29天"之类的事情了。

毕竟,给一个日期加上一个月比加上30*24*3600秒要难一点…


您还可以使用 DateTime 类和DateTime::modify()DateTime::add()等方法。

不会

strtotime('+1 month', $value_from_db); 

工作吗?

取决于数据库。

    MySQL有TIMESTAMPADD
  • Postgre允许你添加INTERVAL 1个月
  • 甲骨文……实在是太烦人了。但是你可以在这里找到怎么做。

(不知道其他的,抱歉)

当然在PHP中也有strtotime('+1 month', $current_time);。当然,strtotime非常简单,您可能会认为它是作弊;-).

不如这样写:

tm *currTime = localtime(timestamp);
currTime->tm_mon++;
timestamp = mktime(currTime);