我的数据库中有两列:start_date
和 end_date
,它们都是 DATE
类型。我的代码更新日期如下:
$today_date = date("Y-m-d");
$end_date = date("Y-m-d"); // date +1 month ??
$sql1 = "UPDATE `users` SET `start_date` = '".$today_date."', `end_date` = '".$end_date."' WHERE `users`.`id` ='".$id."' LIMIT 1 ;";
使 $end_date
等于 $start_date
+ 一个月的最佳方法是什么?例如,2000-10-01 将变为 2000-11-01。
最佳答案
您可以使用 PHP 的 strtotime()功能:
// One month from today
$date = date('Y-m-d', strtotime('+1 month'));
// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
请注意,+1 个月
并不总是直观计算的。它似乎总是添加当前月份中存在的天数。
Current Date | +1 month
-----------------------------------------------------
2015-01-01 | 2015-02-01 (+31 days)
2015-01-15 | 2015-02-15 (+31 days)
2015-01-30 | 2015-03-02 (+31 days, skips Feb)
2015-01-31 | 2015-03-03 (+31 days, skips Feb)
2015-02-15 | 2015-03-15 (+28 days)
2015-03-31 | 2015-05-01 (+31 days, skips April)
2015-12-31 | 2016-01-31 (+31 days)
您可以使用的其他一些日期/时间间隔:
$date = date('Y-m-d'); // Initial date string to use in calculation
$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+2 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 month', strtotime($date)));
$date = date('Y-m-d', strtotime('+30 days', strtotime($date)));
关于php - 如何从今天的日期获取下个月的日期并将其插入我的数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4318991/