php 如何在给定日期的未来一个月内获取 Unix 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6753733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:09:09  来源:igfitidea点击:

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

php

提问by FinalForm

Suppose I pull a field from the database, a timestamp field.

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

How do I get the Unix Timestamp, 1 month in the future, from that timestamp field?

如何从该时间戳字段获取未来 1 个月的 Unix 时间戳?

My brain is a bit tired, so I need a bit of a re-jogging.

我的大脑有点累,所以我需要重新慢跑。

回答by jondavidjohn

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

Using strtotime()you can pass it '+1 month' to add a context sensitive month.

使用strtotime()您可以传递“+1 个月”以添加上下文敏感的月份。

回答by Pascal MARTIN

I would use the strtotime()function, to add +1 monthto your timestamp :

我会使用该strtotime()功能,添加+1 month到您的时间戳:

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


With strtotime(), you don't have to think yourself about stuff like "some month avec 30 days, and others have 31", or "february sometimes has 28 days and sometimes 29".


使用strtotime(),您不必考虑诸如“某个月 avec 30 天,而其他月 31 天”或“二月有时是 28 天有时是 29 天”之类的事情。

After all, adding one month to a date is a bit harder that adding 30*24*3600 seconds...

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


You could also work with the DateTimeclass, and methods such as DateTime::modify()or DateTime::add().


您还可以使用DateTime类和方法,例如DateTime::modify()DateTime::add()

回答by cwallenpoole

Depends on the database.

取决于数据库。

(Don't know the others, sorry)

(其他的不知道,抱歉)

Of course there is also strtotime('+1 month', $current_time);in PHP. Of course, strtotime is so incredibly easy you might consider it cheating ;-).

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

回答by iLLin

Wouldn't

不会

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

work?

工作?

回答by Ryland

How about something like:

怎么样:

tm *currTime = localtime(timestamp);

currTime->tm_mon++;

timestamp = mktime(currTime);