Php 过了一段时间,你能知道下星期天的日期吗?

Php 过了一段时间,你能知道下星期天的日期吗?,php,date,Php,Date,我需要在某个截止点之后返回下个星期天的日期。例如,我正在运行一个竞赛网站,截止时间是每周周日晚上10点,因此,如果用户在周日晚上10点之后查看该网站,则需要显示下周的日期 目前我正在使用这个: date('fjs',strotime('this Sunday',strotime('date('fjs',time()))) 这很好,但只能在午夜后工作,所以只能在周一00:00显示下周日的日期,而我需要在周日22:00显示 非常感谢您的帮助 当您的代码返回下个星期日@00:00:00的时间戳时,在检

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

目前我正在使用这个:

date('fjs',strotime('this Sunday',strotime('date('fjs',time())))

这很好,但只能在午夜后工作,所以只能在周一00:00显示下周日的日期,而我需要在周日22:00显示


非常感谢您的帮助

当您的代码返回下个星期日@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

另外,您不需要使用第二个
strotime
,因为它只会生成与第一个
strotime
假设的
now
等效的时间。无论如何,

您首先需要检查今天是否是星期天,以及时间是否小于晚上10点:

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

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

像这样简单的事情就够了吗

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

你不能用一个能满足你需求的时区来混日子。希腊是GMT+2,@BobNocraz感谢DV,我对你的问题投了更高的票,因为它似乎考虑到了我错过的东西,即如果已经是星期天,你所在的地方。删除了我以前的评论。我同意这是一个可行的答案。您可以通过在函数中包含时间来改进
strotime
行:
strotime($when Sunday 22:00:00”)但那只是我的挑剔=/@BobNocraz Pick away,你在我错过一个主要要求的答案上拉我上来是正确的。我更喜欢这个答案,因为它太简单了!如果您认为星期天是一周中的最后一天,那么添加两个小时将使您进入下一周(只有在星期天>=10pm的情况下)而不是使用硬编码的日期,我会将
$CompetitionAdline
构造为带有“next sunday 10pm”的DateTime对象。除此之外,这种方法对我来说很好。@Oldskool如果你完全正确,我会用你的建议更新OP,^编辑得好。如果今天不是星期天,或者是星期天,时间少于22小时,会发生什么?不过,它的可读性要比其他答案好,因为我们今天都会在这里发现自己的错误,这是理所当然的。此代码生成以下错误
注意:未定义变量:22:00后除星期日以外的任何一天的第11行上的CompetitionAdline
在时间检查中添加了另一个次要(但重要)编辑,以确保比赛日期从22:00开始(而不是23:00)得到“快进”。当然提问者早已离开,不会接受我们现在的任何原始答案。Hase la vie仍然保持清洁,这是最重要的thing@RiggsFolly至少,如果他们真的回来了,他们有几个好的选择=)
$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');