Twitter | Facebook | Instagram | YouTube | LinkedIn

Home >> Blog >>

How To Change Page Content Depending On Time and Date in PHP

By Adam Patel on May 20th, 2021

Automated ways of making a website look “alive” are always good. One such way for a service provider that has opening hours is to create an automated open and closed sign.

So when the shop or studio or whatever is open, the website shows that it’s open, and if it’s not, the website shows that it’s closed.

I’m going to show my code for doing this in PHP, because that will be useful to WordPress programmers (and that’s what I happened to be writing in when I faced this challenge).

What needs to happen?

  1. We need to grab the time right now (at the time the user downloaded the web page).
  2. We need to grab the day today.
  3. We need to know the opening and closing times.
  4. We need to know what days the studio or shop or cafe or whatever is open on.

In PHP, we use date() for most of this.

So first, we’ll grab the time right now and assign it to a variable.

$time_now = date("H:i");

That will return the time in 24-hour clock format.

Then we’ll grab the day and assign it to a variable…

$today = date("D");

The “D” here returns a three letter format. e.g. Mon, Tue, Wed, etc. The important thing about this is that it will cycle every week.

Right, if there was a hard part to this task, we’ve just done it. The next steps are to assign to variables the constant information:

$opening_time = "09:00";
$closing_time = "18:00";

And now, putting it all together, all we need is a bit of if/else logic:

if($today == "Mon" || "Tue" || "Wed" || "Thu" || "Fri") {

	if( $closing_time > $time_now && $time_now > $opening_time){
		$status = "We're open!";
	} else {
		$status = "We're closed!";

} else {
	$status = "We're closed!";

So, to break that down, we’re using a nested if statement, first checking the day is one of the days the establishment is open. Between the day strings we are using the OR operator. So in English, it says “if today is a Monday or Tuesday or Wednesday or Thursday or Friday…”

If it is a weekday, run the next if statement, otherwise assign the string “We’re closed!” to the variable $status.

Let’s have a look at the next if statement.

Here, what we want to establish is whether or not the current time, assigned to the $time_now variable is between the opening time and the closing time.

PHP wouldn’t let me write it in one statement, so I’ve used an AND operator between two statements.

So first it checks whether the closing time is greater than the current time and then it checks if the current time is greater than the opening time. Because of the AND operator, BOTH statements must be true for the if statement to return true.

And that’s it!

All that remains is for us to echo the $status variable somewhere in our page so it’s visible to the user.

<?php echo $status ; ?>

And that’s job done.

We could use the same principle with a few code tweaks to satisfy lots of different use cases like, for example, wanting a particular part of a page to change depending on the month of the year that it is.