简体   繁体   中英

Easiest way to get next monday from current day?

I am looking for easiest method to find next Monday or some other day for example in php using mktime() or DateTime()

Sample Data -> Result

Today is 15th Feb 2016 - Monday -> 22nd Feb 2016 - Monday
Today is 16th Feb 2016 - Tuesday -> 22nd Feb 2016 - Monday
Today is 22nd Feb 2016 - Monday -> 29th Feb 2016 - Monday
Today is 24th Feb 2016 - Wednesday -> 29th Feb 2016 - Monday

I have a preference to the DateTime() object personally because I find it a lot more flexible than date() . In the example below you have a stored DateTime() object that you can easily perform further modifications/formats on as you see fit rather than if you use date() .

Here's an example of its usage:

$date = new DateTime();
$date->modify('next monday');

echo $date->format('Y-m-d H:i:s');

You can find a list of supported DateTime formats here which you can use, a few examples being: next week , +2 day , -3 month .

For outputting the date you can find a list of formats here

Any more information you want, you're welcome to ask away. ^^

好的,用这个

$nextMonday = strtotime('next monday');
echo date('Y-m-d', strtotime('next monday'));

Refer http://php.net/manual/en/function.date.php for getting the exact format of date. I have just used the basic Ymd format

Although I find date function very easy to use, but go for the other answer if you like a OO approach to your code.

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