PHP在当前时间()上添加月和日,并取消周末


PHP adding months and days to the current time() and ovoid weekend days

我需要在PHP中添加N个月和M天的当前时间()。我目前的解决方案很简单,但我想知道它可以更简单:)?

$my_time = strtotime("+6 days", strtotime("+1 month", time()));

我也想知道是否有机会在目标日期避免周末(周六和周日)?

是的,您可以简化为:

$my_time = strtotime("+6 days 1 month", time());

或:

$my_time = strtotime("+6 days 1 month");

看到

我会使用DateTime类,因为它比strtotime快得多。

$date = new DateTime();
$date->add(new DateInterval('P1M6D'));
echo $date->format('Y-m-d');

DateInterval的详细语法信息可以在这里找到。

尝试以下方法 使用strtotime()函数添加日、月、年

For Day adddate('Y-m-d', strtotime(' +3 days '));添加

date('Y-m-d', strtotime("+3 month"));

年添加

date('Y-m-d', strtotime("+3 year"));

对于日、月、年添加

date('Y-m-d', strtotime("+1 days 1 month 1 year "));

For subtract day month year

date('Y-m-d', strtotime("-1 days 1 month 1 year "));

只使用( + - )符号