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

时间:2016-02-09 10:18:57

标签: php date

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

目前我正在使用它:

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

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

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:2)

像这样的简单就足够了吗?

$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');

答案 1 :(得分:1)

当您的代码返回下一个星期日@ 00:00:00的时间戳时,在检查您是否已经在星期日之前以及截止时间之前或之后,只需添加22小时。

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

给予

14/02/2016 22:00:00

此外,您不需要使用第二个strtotime,因为它只生成now的等效值,这是第一个strtotime所假设的

答案 2 :(得分:1)

你需要检查今天是否是星期日,如果小时是否小于晚上10点:

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

现在在你的strtotime中使用那个变量:

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

3v4l proof of concept

答案 3 :(得分:1)

试试这个,

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