I have a delivery service that only picks up Monday through Thursday. I only want to give the user the next available three days as an option for a scheduled pickup.
My code works, but I was wondering if there was a more "efficient" way to write what Im trying to achieve?
$numericDay=date('N');
if ($numericDay==1) {
echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\o\w - F j, Y', strtotime('+1 days')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('+2 days')) . '">' . date('l - F j, Y', strtotime('+2 days')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('+3 days')) . '">' . date('l - F j, Y', strtotime('+3 days')) . '</option>';
}
if ($numericDay==2) {
echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\o\w - F j, Y', strtotime('+1 days')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('+2 days')) . '">' . date('l - F j, Y', strtotime('+2 days')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';
}
if ($numericDay==3) {
echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\o\w - F j, Y', strtotime('+1 days')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('+next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('next tuesday')) . '">' . date('l - F j, Y', strtotime('next tuesday')) . '</option>';
}
if ($numericDay>=4 and $numericDay<=7) {
echo '<option value="' . date('Ymd', strtotime('next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('next tuesday')) . '">' . date('l - F j, Y', strtotime('next tuesday')) . '</option>';
echo '<option value="' . date('Ymd', strtotime('next wednesday')) . '">' . date('l - F j, Y', strtotime('next wednesday')) . '</option>';
}
Thanks in advance for any insight!!!
Here is what you need ...
$dateTime = new DateTime();
$x = 0;
while ( $x < 3 ) {
$dateTime->modify("+1 day");
if ($dateTime->format("N") > 4)
continue;
printf("<option value=\"%s\">%s</option>
", $dateTime->format("Ymd"), $dateTime->format("l - F j, Y"));
$x ++;
}
Output
<option value="20130225">Monday - February 25, 2013</option>
<option value="20130226">Tuesday - February 26, 2013</option>
<option value="20130227">Wednesday - February 27, 2013</option>
Create a while loop:
$echoedDays = 0;
$dateIndex = 0
while ($echoedDays < 3) {
Start from tomorrow's date and check the day number. If it's Mon-Thu (1-4), echo the date and increase $echoedDays
. Otherwise just increase $dateIndex
and move to the next date.
$store = array(
1 => array(
date('Ymd', strtotime('+1 days')),
date('\T\o\m\o\o\w - F j, Y', strtotime('+1 days'))
),
array(
date('Ymd', strtotime('+2 days')),
date('\T\o\m\o\o\w - F j, Y', strtotime('+2 days'))
),
array(
date('Ymd', strtotime('+3 days')),
date('\T\o\m\o\o\w - F j, Y', strtotime('+3 days'))
),
'm' => array(
date('Ymd', strtotime('next monday')),
date('l - F j, Y', strtotime('next monday'))
),
't' => array(
date('Ymd', strtotime('next tuesday')),
date('l - F j, Y', strtotime('next tuesday'))
),
'w' => array(
date('Ymd', strtotime('next wednesday')),
date('l - F j, Y', strtotime('next wednesday'))
)
);
$numericDay=date('N');
if ($numericDay==1) {
echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
echo '<option value="' . $store[2][0] . '">' . $store[2][1] . '</option>';
echo '<option value="' . $store[3][0] . '">' . $store[3][1] . '</option>';
}
elseif ($numericDay==2) {
echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
echo '<option value="' . $store[2][0] . '">' . $store[2][1] . '</option>';
echo '<option value="' . $store['m'][0] . '">' . $store['m'][1] . '</option>';
}
elseif ($numericDay==3) {
echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
echo '<option value="' . $store['m'][0] . '">' . $store['m'][1] . '</option>';
echo '<option value="' . $store['t'][0] . '">' . $store['t'][1] . '</option>';
}
elseif ($numericDay>=4 and $numericDay<=7) {
echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
echo '<option value="' . $store['t'][0] . '">' . $store['t'][1] . '</option>';
echo '<option value="' . $store['w'][0] . '">' . $store['w'][1] . '</option>';
}
This could also work:
$numericDay=date('N');
if($numericDay < 4)
echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\o\w - F j, Y', strtotime('+1 days')) . '</option>';
else
echo '<option value="' . date('Ymd', strtotime('next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';
if($numericDay < 3)
echo '<option value="' . date('Ymd', strtotime('+2 days')) . '">' . date('l - F j, Y', strtotime('+2 days')) . '</option>';
else
echo '<option value="' . date('Ymd', strtotime('next tuesday')) . '">' . date('l - F j, Y', strtotime('next tuesday')) . '</option>';
if($numericDay < 2)
echo '<option value="' . date('Ymd', strtotime('+3 days')) . '">' . date('l - F j, Y', strtotime('+3 days')) . '</option>';
else
echo '<option value="' . date('Ymd', strtotime('next wednesday')) . '">' . date('l - F j, Y', strtotime('next wednesday')) . '</option>';
Here's what I would do.
$offsets = array(
1 => array(1, 2, 3),
2 => array(1, 2, 6),
3 => array(1, 5, 6),
4 => array(4, 5, 6),
5 => array(3, 4, 5),
6 => array(2, 3, 4),
7 => array(1, 2, 3)
);
$numericDay = date('N');
foreach ($offsets[$numericDay] as $offset)
{
$dateFormat = $offset == 1 ? '\T\o\m\o\o\w - F j, Y' : 'l - F j, Y';
echo '<option value="' . date('Ymd', strtotime('+' . $offset . ' days')) . '">' . date($dateFormat, strtotime('+' . $offset . ' days')) . '</option>';
}
Edit: Tidied it up a bit.
$days = Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
for($i=1;$i<=7;$i++){
if($i > 4){ $n = 1; } else {
$n = $i%4+1;
}
$o = ($n)%4+1;
$p = ($o)%4+1;
echo "It's ".$days[$i-1]." and the next three available days are... ".$days[$n-1].", ".$days[$o-1]." and ".$days[$p-1]."
";
}
It's Mon and the next three available days are... Tue, Wed and Thu
It's Tue and the next three available days are... Wed, Thu and Mon
It's Wed and the next three available days are... Thu, Mon and Tue
It's Thu and the next three available days are... Mon, Tue and Wed
It's Fri and the next three available days are... Mon, Tue and Wed
It's Sat and the next three available days are... Mon, Tue and Wed
It's Sun and the next three available days are... Mon, Tue and Wed