我需要使用 PHP 对我的 MySQL 数据库中的时间和日期值进行更复杂的计算。
我需要从给定日期添加或减去不同的值。
例如:
- 减去 1 个月
- 减去 30 天
- 减去 4 周
- 添加 4 个月
- 添加 3 个月
- 增加 90 天
- 增加 2 年
请注意,减去 1 个月、4 周或 30 天之间存在差异。
执行此操作的首选方法是什么?是否有任何聪明的库或者我可以用 PHP 自己的函数来做到这一点?
最佳答案
来自 http://php.net/manual/en/function.strtotime.php
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
您可以添加第二个参数以使其添加到给定时间:
echo strtotime("+1 week",$timestamp)
关于php - 如何用 PHP 处理日期和时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4101071/