繁体   English   中英

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

[英]Get date of next Sunday after a certain time?

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

目前我正在使用这个:

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

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

任何帮助深表感谢!

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

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

当您的代码返回下一个星期日@ 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 ,因为strtotime产生了now的等价物,这是第一个strtotime所假设的

您需要检查今天是否是星期日,如果小时是否小于晚上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概念证明

试试这个,

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

暂无
暂无

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

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