Get the next Sunday date after a specific time?

I need to return the date of the next Sunday after a certain cut-off point. For example, I launch a site for competitions, and a slice is 10 pm per week every week, so if a user views the website after 10 pm on Sunday, he would need to display the next week.

I am currently using this:

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

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

Any help is much appreciated!

+4
source share
4 answers

Is this simple enough?

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

, @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

+1

You need to check if it is Sunday today and if the hour is less than 10 pm:

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

now use this variable in your strtotime:

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

3v4l proof concept

+1
source

Try it,

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

Source: https://habr.com/ru/post/1627933/


All Articles