简体   繁体   English

在PHP下一个星期一获得

[英]Get next to next monday in PHP

How can i get 2nd monday after the input date? 输入日期后第二个星期一如何获得? i have solution but that not feasible 我有解决方案,但不可行

$date = new DateTime();
$date->modify('next monday');
$next_monday = $date->format('Y-m-d');
$date = new DateTime($next_monday);
$date->modify('next monday');
$next_monday = $date->format('Y-m-d');

Please suggest me some method to do so. 请建议我一些方法。

Your DateTime object's modify method takes the same type of arguments that strtotime does. DateTime对象的modify方法采用与strtotime相同类型的参数。 You're already using 'next monday' , you just need to use 'second monday' instead. 您已经在使用'next monday' ,而只需要使用'second monday'

$date->modify('second monday');
echo $date->format('Y-m-d');

Also, in case you didn't know this, some of the DateTime methods can be chained: 另外,如果您不知道这一点,可以将某些DateTime方法链接起来:

echo $date->modify('second monday')->format('Y-m-d');

You can do it with strtotime() but if you think it is too costly, you can use date and DateInterval as well. 您可以使用strtotime()但是如果您认为它太昂贵了,则可以使用dateDateInterval

$date = new DateTime('2017-02-15 13:03:00');
// move back to past Monday
$num = (date("w", $date->getTimestamp())) - 1;
$offset = new DateInterval("P{$num}D");
$offset->invert = 1;
// move forward two weeks
$interval = new DateInterval('P2W');
$next_second_monday = $date->add($offset)->add($interval);

And $next_second_monday will be: $next_second_monday将是:

DateTime Object
(
    [date] => 2017-02-27 13:03:00.000000
    [timezone_type] => 3
    [timezone] => UTC
)

There is probably another way to do so but using MomentPHP You could get the start of today's week, adding 1 day ( to get to monday ) and then adding two week, you would get to the second next monday. 可能还有另一种方法,但是使用MomentPHP,您可以从今天的星期开始,添加1天(到星期一),然后再加上两周,那么您将到达下一个星期一。
Something like that : 像这样:

<?php
$m = \Moment\moment();
$sunday = $m->startOf('week');
$monday = $sunday->addDays(1);
$2nextMonday = $monday->addWeeks(2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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