Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFrank Jimenez
5,498 PointsHelp. PHP and comparing days of the week
Hi all,
I am working on a proyect. I'm building a website and I want to show a simple phrase - the next day we are open depending on the current day.
For example, the days I open my shop is Tuesday and Saturday. So if someone goes into the website on Sunday can see display on top: "Next Day Open: Tuesday". Or if someone goes into the website on Thursday, the website displays automatically the nearest day we are open, in this case "Next Day Open: Saturday".
Can someone give me a hand?
3 Answers
Chris Shaw
26,676 PointsHi Frank,
In PHP we can achieve this by using the date
function with the l modifier, this gives us the full name of a day based on the servers clock. The below code simply checks the string given from date
and determines which day is needed based on a group of case statements.
In this code I've added Tuesday and Saturday to their own groups mainly because there would be a lot more code involved to do true day-by-day determinations.
<?php
switch (date('l')) {
case 'Sunday':
case 'Monday':
case 'Tuesday':
$nextOpen = 'Tuesday';
break;
case 'Wednesday':
case 'Thursday':
case 'Friday':
case 'Saturday':
$nextOpen = 'Saturday';
break;
default:
$nextOpen = 'Unknown';
}
echo $nextOpen;
If you want to use a custom timezone you can use the date_default_timezone_set function which accepts a timezone string.
Hope that helps.
jordans
1,609 PointsIf the alerts you're putting out depending on the day aren't specific and stay the same every week, all you need is JavaScript.
With Javascript, you can create an Array with 7 variables that each represent a day and each have something specific it'll print out. Then you can create a variable that'll allow you to get today's date. After that, create another variable that'll get today's day as a number 0-7 and that's pretty much it.
Just write the array name with the currentdate variable inside the brackets to a div class and you're set. I wrote a really simple example for you below.
function Array (n) {
this.length = n;
for (var i =1; i <= n; i++) {
this[i] = ' '
}
}
day = new Array(7);
day[0] = 'Today is Sunday.';
day[1] = 'Today is Monday.';
day[2] = 'Today is Tuesday.';
day[3] = 'Today is Wednesday.';
day[4] = 'Today is Thursday.';
day[5] = 'Today is Friday.';
day[6] = 'Today is Saturday.';
var currentdate = new Date();
var daynumber = currentdate.getDay();
document.querySelector('.results').innerHTML = day[daynumber]);
<div class="results"></div>
View it live over at jsfiddle
Frank Jimenez
5,498 PointsThank you Jordanportfolio and Chrisupjohn for the quick reply.
I found particularly helpful your explanation Chris Upjohn. I already included the code on the website and it's working perfectly.
Chris Shaw
26,676 PointsYou're welcome