Reputation: 497
I have a very complicated delivery situation with a switch statement that is checking day of the week and a cutoff time of 16:00 everyday and echo's out the delivery day which on Monday, Tuesday and Wednesday is todays day + 1 day if post cut off it should echo todays date + 2, thursdays and fridays have completely different delivery days due to the none delivery on weekends
<?php
$today = date("D");
switch($today){
case "Mon":
if(mktime(16, 0, 0) <= time()) {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 2 days')) . "</strong><p>";
} else {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
}
break;
case "Tue":
if(mktime(16, 0, 0) <= time()) {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 2 days')). "</strong><p>";
} else {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
}
break;
case "Wed":
if(mktime(16, 0, 0) <= time()) {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 2 days')). "</strong><p>";
} else {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
}
break;
case "Thu":
if(mktime(16, 0, 0) <= time()) {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 4 days')). "</strong><p>";
} else {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
}
break;
case "Fri":
if(mktime(16, 0, 0) <= time()) {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 5 days')). "</strong><p>";
} else {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 4 days')). "</strong><p>";
}
break;
case "Sat":
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 4 days')). "</strong><p>";
break;
case "Sun":
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 3 days')). "</strong><p>";
break;
default:
echo "No information available for that day.";
break;
}
?>
surely they must be a more elegant way of achieving this, would any body know of a cleaner way of achieving the same result. This code works but could be alot better and shorter
Upvotes: 0
Views: 5092
Reputation: 53601
Note you can stack case
matches that do the same thing:
switch ($today) {
case 'Mon':
case 'Tue':
case 'Wed':
// code here
break;
}
But you're better off just making a little array that contains the values you need for the days, and then look up today's value by using the day name as the array index:
$leadtimes = [
'Mon' => [1, 2], // first value is for before 16:00, second is for after
'Tue' => [1, 2],
'Wed' => [1, 2],
'Thu' => [1, 4],
'Fri' => [4, 5],
'Sat' => [4, 4],
'Sun' => [3, 3],
];
Then, the code to calculate the lead time is just one line:
$leadtime = $leadtimes[date('D')][date('H') < 16 ? 0 : 1];
Then plug it into your output as previous:
echo
"<p>For Delivery on <strong> " .
date('D jS', strtotime("$Date + $leadtime days")) .
"</strong><p>";
Now, when you want to change leadtimes, there's no coding to edit, just adjust the value in the array.
And because it's been a while since I've done any code golf, here's the whole thing on a single line:
echo date('D jS',strtotime('+'.['Mon'=>[1,2],'Tue'=>[1,2],'Wed'=>[1,2],'Thu'=>[1,4],'Fri'=>[4,5],'Sat'=>[4,4],'Sun'=>[3,3]][date('D')][date('H')<16].'day'));
Upvotes: 2
Reputation:
How about this:
$today = date("D");
$cutoff = mktime(16, 0, 0) <= time();
$offset = $cutoff ? 2 : 1;
$offset = $cutoff and $today === 'Thu' ? 4 : 1;
$offset = $cutoff and $today === 'Fri' ? 5 : 4;
$offset = $today === 'Sat' ? 4 : $offset;
$offset = $today === 'Sun' ? 3 : $offset;
echo "<p>For Delivery on <strong> " . date('D jS', strtotime("$Date $offset days")) . "</strong><p>";
Also I'd suggest using Carbon for working with dates.
Upvotes: 0
Reputation: 57131
A quick attempt would be as I mentioned in the comment, remove the repeating code, this is both the string used to echo and also being able to organise the switch so that any days with common values are all processed by the same code...
$today = date("D");
switch($today){
case "Mon":
case "Tue":
case "Wed":
$days = (mktime(16, 0, 0) <= time())? 2 : 1;
break;
case "Thu":
$days = (mktime(16, 0, 0) <= time())? 4 : 1;
break;
case "Fri":
$days = (mktime(16, 0, 0) <= time())? 5 : 4;
break;
case "Sat":
$days = 4;
break;
case "Sun":
$days = 3;;
break;
default:
break;
}
if ( isset($days) ) {
echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. " + {$days} days")). "</strong><p>";
}
else {
echo "No information available for that day.";
}
Upvotes: 1