I am making a website for a store that wants to show the next open sunday dynamically based on date. I managed to make a script that does so but only tests one date. I want to test 10 dates without putting them in a database. This is my script.
<?
$mydate = '2012-12-23';
$curdate = date('Y-m-d');
if($curdate == $mydate)
{
echo 'Vandaag';
}
elseif($curdate > $mydate)
{
echo '26 december';
}
elseif($curdate < $mydate)
{
echo '23 december';
}
else
{
echo 'Koopzondag niet ingeladen';
}
?>
Maybe this is what you are looking for:
<?php
$mydate = '2012-12-23';
echo date('d F', strtotime(getNextSunday($mydate)));
function getNextSunday($date) {
$timestamp = strtotime($date);
if (date('w', $timestamp) == 0) {
$sunday = date('Y-m-d', $timestamp); // today
}
else {
$sunday = date('Y-m-d', strtotime('next Sunday', $timestamp)); // next sunday
}
return $sunday;
}
If you have a list of open Sundays in an array (when using a DB, the whole thing changes wildly, you will have the date functionalities of the DB at your disposal), you could do this:
$opensundays = array("2012-12-23", "2012-12-30", "2012-01-06");
$today = date("Y-m-d");
$r = "";
foreach ($opensundays as $idx=>$sunday) {
if ($today == $sunday) {
$r = "today is an open Sunday";
break;
} elseif ($today > $opensundays[$idx-1] && $today < $sunday) {
$r = "next open Sunday is at ".$sunday;
break;
}
}
Note that this code has no boundary checks, it is just to show a concept.