简体   繁体   中英

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

I need to add N months and M days to the current time() in PHP. My current solution is simple, but I was wondering can it be even simpler :)?

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

I am also wondering if there is a chance to avoid weekends (Saturday and Sunday) in the target date?

Yes, you can simplify it to:

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

Or:

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

See this

I would use the DateTime class, because it is much faster than strtotime.

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

Detailed synthax info for the DateInterval can be found here .

Try this following Methods use strtotime() function add days months years

For Day add date('Ym-d', strtotime("+3 days")); For month add

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

year add

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

For day month year add

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

just use ( + and - ) symbols

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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