by Colin

Conditional statements to find & use the day of the week in WordPress

We were working on a recent project where we had to figure out what day of the week it was and present something on the frontend of the website specifically for each day. The conditional script could be used to change any aspect of a website design based on the day of the week.

It is important to change the settings in WordPress under “Settings” > “General” and next to “Timezone” to be accurate to the timezone of your website. Once you do this, and use the code below, the script will accurately display or do whatever you echo in the php script. You can of course also use a php switch for this if you like.

Note about the timezone : we set the timezone directly in the script because working with server times and the WordPress timezone setting can be a little weird we’ve found, so it’s best to set the timezone directly.

date_default_timezone_set('America/New_York'); // Put your PHP supported timezone in place of America/New York
$script_tz = date_default_timezone_get();
// get current day:
$currentday = date('l');
if ($currentday == Monday){
echo '
Monday
'; } elseif ($currentday == Tuesday){ echo '
Tuesday
'; } elseif ($currentday == Wednesday){ echo '
Wednesday
'; } elseif ($currentday == Thursday){ echo '
Thursday
'; } elseif ($currentday == Friday){ echo '
Friday
'; } elseif ($currentday == Saturday){ echo '
Saturday
'; } elseif ($currentday == Sunday){ echo '
Sunday
'; } else{ //what, you were looking for other days? =/ }