Reputation: 201
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!
Upvotes: 4
Views: 2481
Reputation: 1196
While there is a good accepted answer already for this problem, I thought it worthwhile to add the quick solution to finding the time on next Sunday at 22:00 (your original question).
The strtotime() function does the work if you add the time to the required day of the week as in:
$stamp = strtotime('Next Sunday + 22 hour');
echo date('D Y-m-d H:i:s', $stamp);
I hope that this helps someone to easily get a timestamp for a particular day and time.
Upvotes: 0
Reputation: 2704
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');
Upvotes: 4
Reputation: 94642
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
Upvotes: -1
Reputation: 772
Try this,
echo date('F jS', strtotime('this Sunday', time() + (2*60)));
Upvotes: 1
Reputation: 446
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"));
Upvotes: 2