php PHP通过将天数添加到可变日期来计算未来日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2692679/
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-25 07:22:41  来源:igfitidea点击:

PHP Calculating future date by adding days to a variable date

phpmathdate

提问by OneNerd

I was looking at this post, and it is close to what I need: PHP - How to count 60 days from the add date

我正在看这篇文章,它接近我需要的内容: PHP - 如何从添加日期算起 60 天

However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).

但是,在该帖子中,计算是通过将 60 天添加到当前日期来执行的。我需要做的是根据可变日期(而不是当前日期)计算日期

Something like this:

像这样的东西:

$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;

Anyone know how to do that?

有谁知道怎么做?

Thanks

谢谢

回答by Michael Mrozek

You can put something before the "+10 days" part:

你可以在“+10天”部分之前放一些东西:

strtotime("2010-01-01 +10 days");

回答by jpabluz

Use date_add

使用 date_add

http://www.php.net/manual/en/datetime.add.php

http://www.php.net/manual/en/datetime.add.php

$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));

回答by Faisal

You will have to look into strtotime(). I'd imagine your final code would look something like this:

您将不得不研究strtotime()。我想你的最终代码会是这样的:

$dateVariable      = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;

If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:

如果您使用的是 PHP >= 5.2,我强烈建议您使用新的 DateTime 对象。例如像下面这样:

$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");

回答by Santin

I see you are retriving data from a database. If you are using mysql you can do it on the select:

我看到您正在从数据库中检索数据。如果您使用的是 mysql,则可以在选择上执行此操作:

Example: you need the last date of the table and this date-7 days

示例:您需要表的最后日期和此日期-7 天

select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table

It′s easy use curdate() if you want todays date.

如果你想要今天的日期,使用 curdate() 很容易。

If you need a dynamic between that selects the count of last 7 days:

如果您需要动态选择过去 7 天的计数:

select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"

回答by R T

date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))

this will get the calculated date in defined format.

这将获得定义格式的计算日期。

回答by sandeep kasar

Suppose today's date is

假设今天的日期是

date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");

And i can add 10 days in current date as follows :

我可以在当前日期添加 10 天,如下所示:

$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));