使用PHP从MySQL添加天数到当前日期

时间:2022-04-02 19:09:43

I have a fixed date from MySql

我有一个MySql的固定日期

startDate = 07/03/2011

I wanted to add 60 days on top this date to have an endDate.

我想在这个日期添加60天以获得endDate。

$startDate = $result['startDate'];
$endDate = ??? + strtotime("+60 days");
echo $endDate;

From my research, I know it has something do with strtotime, but all the sites I come across with based the start date from current workstation's time. My date is already fixed and entered prior to running and getting the endDate.

根据我的研究,我知道它与strtotime有关,但我遇到的所有网站都基于当前工作站的开始日期。我的日期已经修复并在运行和获取endDate之前输入。

Help? Thanks in advance!

救命?提前致谢!

4 个解决方案

#1


8  

In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:

除了其他人提供的PHP解决方案之外,您还可以在MySQL内部创建endDate并省去一些麻烦:

SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;

-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;

Relevant documentation here...

这里的相关文件......

#2


5  

You could reformat the results of strtotime()

你可以重新格式化strtotime()的结果

$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));

Demo: http://codepad.org/9rWnoeQb

#3


2  

$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;

#4


1  

86400 seconds in a day, times number of days.. and add it to current time.

一天86400秒,乘以天数..并将其添加到当前时间。

$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);  

#1


8  

In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:

除了其他人提供的PHP解决方案之外,您还可以在MySQL内部创建endDate并省去一些麻烦:

SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;

-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;

Relevant documentation here...

这里的相关文件......

#2


5  

You could reformat the results of strtotime()

你可以重新格式化strtotime()的结果

$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));

Demo: http://codepad.org/9rWnoeQb

#3


2  

$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;

#4


1  

86400 seconds in a day, times number of days.. and add it to current time.

一天86400秒,乘以天数..并将其添加到当前时间。

$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);