在一段时间后获取下个星期天的日期?

在一段时间后获取下个星期天的日期?

问题描述:

I need to return the date of the next Sunday after a certain cut-off point. For example - I'm running a competition website and the cut-off is 10PM on Sunday every week, so if a user were to look at the website after 10PM on a Sunday, it would need to display next weeks date.

At the moment I'm using this:

date('F jS', strtotime('this Sunday', strtotime(date('F jS', time()))));

Which is great, but only works past midnight, so will only display the next Sunday's date at 00:00 on Monday, when I need it at 22:00 on Sunday.

Any help is much appreciated!

我需要在某个截止点之后返回下一个星期日的日期。 例如 - 我正在运行一个竞赛网站,截止时间是每周星期日晚上10点,所以如果用户在星期天晚上10点之后查看网站,则需要显示下周的日期。 p >

目前我正在使用它: p>

date('F jS',strtotime('this Sunday',strtotime(date(' F jS',time())))); code> p>

哪个好,但只能在午夜过后,所以只会在00:00显示下一个星期日的日期 周一,我需要在周日的22:00。 p>

非常感谢任何帮助! p> div>

Would something simple like this suffice?

$competitionDeadline = new DateTime('this sunday 10PM');

$date = new DateTime();

if ($date->format('l') === 'Sunday' && $date->format('H') >= '22') {
    // It is past 10 PM on Sunday, 
    // Override next competition dates here... i.e.

    $competitionDeadline = new DateTime('next sunday 10PM');
}

// Wherever you are presenting the competition deadline...
$competitionDeadline->format('Y-m-d H:i:s');

As your code returns a timestamp for next sunday @ 00:00:00 just add 22 hours to it, after checking if you are before it is already Sunday and before or after the cutoff time.

// work out if we what this sunday or next sunday
// based on whether we are before or after the cutoff of 22:00
$when = (date('N') == '7' && date('h') > 22) ? 'this' : 'next';

$comp_finish = strtotime("$when Sunday") + (60*60*22);
echo date('d/m/Y H:i:s', $comp_finish);

Giving

14/02/2016 22:00:00

Also you dont need to use the second strtotime as that just generates the equivalent of now which is assumed by the first strtotime anyway

You need to check if today is a sunday and if the hour is less than 10pm first:

$next = 'next'; //default to 'next', only change if below matches:
if (date('N') == '7' && date('h') < 22) $next = 'this';

now use that variable in your strtotime:

date('F jS', strtotime("$next Sunday"));

3v4l proof of concept

Try this,

echo date('F jS', strtotime('this Sunday', time() + (2*60)));