Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Garconis/883e2e3f7d79fedc6537480d7f175434 to your computer and use it in GitHub Desktop.
Save Garconis/883e2e3f7d79fedc6537480d7f175434 to your computer and use it in GitHub Desktop.
WordPress | Shortcode to output hours based on day of week
<?php
// use examples: [nextdaytime day="+1 day"] [nextdaytime day="+2 day"]
function displaynextdaytime($atts){
$originalZone = date_default_timezone_get(); //save timezone
date_default_timezone_set(get_option('timezone_string')); //set it to your admin value
$time = strtotime($atts['day']); //time for the date() function based on our 'day' attribute
if (date('l', $time) == 'Sunday'){
$statusStr = 'Open';
$timeStr = 'Noon – 5PM';
}
elseif (date('l', $time) == 'Monday'){
$statusStr = 'Closed';
$timeStr = '';
}
elseif (date('l', $time) == 'Tuesday'){
$statusStr = 'Closed';
$timeStr = '';
}
elseif (date('l', $time) == 'Wednesday'){
$statusStr = 'Open';
$timeStr = 'Noon – 5PM';
}
elseif (date('l', $time) == 'Thursday'){
$statusStr = 'Open';
$timeStr = 'Noon – 5PM';
}
elseif (date('l', $time) == 'Friday'){
$statusStr = 'Open';
$timeStr = 'Noon – 9PM';
}
elseif (date('l', $time) == 'Saturday'){
$statusStr = 'Open';
$timeStr = 'Noon – 5PM';
}
// value for initial hours status
$dayStatusVal = 'We are: <span class="hours-status">' . $statusStr . '</span> <span class="hours-day">' . date('D', $time) . '</span>';
// value for time if it has it
if($timeStr !== ''){
$timeVal = ': <span class="hours-time">' . $timeStr . '</span>';
}
else {
$timeVal = '';
}
$returnVal = '<div class="hours-wrapper">'. $dayStatusVal .''. $timeVal . '</div>';
date_default_timezone_set($originalZone); //restore original zone
return $returnVal;
}
add_shortcode('nextdaytime', 'displaynextdaytime');
// end display next day and time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment