简体   繁体   English

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

[英]Add days to current date from MySQL with PHP

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.根据我的研究,我知道它与 strtotime 有关,但我遇到的所有站点都基于当前工作站时间的开始日期。 My date is already fixed and entered prior to running and getting the endDate.我的日期已经确定并在运行和获取 endDate 之前输入。

Help?帮助? Thanks in advance!提前致谢!

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... 相关文档在这里...

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演示: http://codepad.org/9rWnoeQb

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

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);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM