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.

How To Make A Children’s Spelling Game in JavaScript

By Adam Patel on May 18th, 2021

In this experiment I made a spelling game for children.

The working game can be found here.

The game is played as follows:

The player is presented with a picture of something. Using the letters available to them, they must spell out the name of the thing in the picture.

If the player gets it right (by spelling the word correctly), the game will tell them the answer is correct and then load up the next question.

If the player gets it wrong, the game will tell the player that the answer is not correct and recommend they try again.

Letters can be entered in any order.

How It Works:

The drag and drop functionality in the UI, is a means of adding single character text strings into an array.

Every time a letter tile is dropped into a position on the board, the corresponding single character text string is added to the array. The functions are written specifically such that the string is added to the array at the index corresponding to the position on the board (using splice instead of push).

So if there are three positions on the board, and the answer is ‘CAT’, and the player drags the letter A to the middle position on the board first, this will be entered into into the array at position 0 (array indices start counting from 0, not from one), because it is currently the only entry in the array. But when the C is added second, it will go into position 0, pushing the A to position 1. This means letters can be added to the board in any order and the game will still work.

After a letter is added to the array, a second function is called that checks the length of the array. If the length of the array is equal to the number of characters in the answer, the array is checked against the answer to the question.

If the current array is exactly the same as the answer, the game tells the player they are correct and then increases their score by one.

If the answer is incorrect, the game will tell the player that their answer is not correct and tell them to try something else.

To clear the board, the player can use the “reset board” button. This button calls a function which resets all the board position variables to empty and reloads a full set of letters in order that the player can re-attempt the question.

The reset board button can be used for this and any number of other edge cases. e.g. If the player enters a letter in the wrong position on the board. With the current code, it is impossible for the user to move the letter using drag and drop, and attempting to do so will simply reenter the same letter in the array a second time.

How To Write A JavaScript Function

By Adam Patel on May 16th, 2021

black flat screen computer monitor

A function is a bunch of code that runs when called.

Below is the code for the basic structure of a Javascript function.

function nameOfFunction(){
      // write the code here
}

Let’s break it down:

We start with the word ‘function’, followed by a space. This is so that the system knows we’re writing a function.

Next, we have the name of the function written in ‘JavaScript case’ (Don’t Google this – it’s not an official term, just my name for it). It’s not imperative, but it is a well respected convention in Javascript circles. All you do is write the first word in lowercase, followed by camel case for every additional word. No spaces.

Immediately after the name of the function is a pair of brackets like this: (). If the function takes arguments, these must be stated between the brackets. An argument is any type of input data the function requires in order to ‘work’ – for example if you made a function that determines whether a number is divisible by 6 or not, you’d have to provide the number as an argument, for the function to work. The code would look like this:

function divisibeBySeven(num){
        // code here
}

Immediately after the brackets are a pair of curly braces. It is between these curly braces where the Javascript function code will be written.

JavaScript functions can be used for standard programming tasks, for example finding the lowest number in a sequence or fizzbuzz or whatever, as well as DOM manipulation, which is, on the whole, unique to JavaScript.

Experiments in Javascript

By Adam Patel on May 14th, 2021

Lately, I’m really falling in love with Javascript. My early encounters with the language were on the negative side but now I think it’s fantastic.

Drag & Drop Functionality

I’ve been experimenting with drag and drop functionality because of the possibilities it creates for snazzy UIs and really flashy user experiences.

As part of that effort, I’m currently making a spelling game for children which has been fun. (Full write up of that is coming upon completion of version 1).

In the game, the player can drag and drop letters (like Scrabble) into a bunch of board positions (like hangman) in order to spell out a word which is the answer to a question.

Keyboard Events

Taking a thought for a walk to see if I could get the letters to jump on the board by pressing the corresponding letter on the keyboard opened up a whole new area for exploration as I discovered and implemented keyboard events.

The possibilities here are pretty endless. I can see myself building both office applications and games pretty soon using this.

How To Make Social Media Link Previews That Get Clicked

By Adam Patel on May 13th, 2021

There are two major ways of getting traffic if you’re not going to pay for it: You can get traffic from search engines, usually via SEO; or you can get traffic from social media networks.

In this article we’re looking at social media and how to make links that get clicked.

When somebody copies our link into Facebook, Facebook creates a link preview of that page.

If we want anybody to click through to our website, we’ve got to make that link preview attractive – because at this point, that link preview represents the entire website and people are going to judge it and assume the website is of the same quality as the link preview.

By default, Facebook will grab the first image in the HTML code and the title and description tags from the head of the page. But those tags exist primarily for Google and other search engines. And very often, text chosen so that a search engine will give us traffic isn’t the same text that will influence a human reader to click through to the link.

Wouldn’t it be nice if we could write some code just for Facebook, to tell the link preview what to say? Well… funny you should ask because Facebook OpenGraph tags exist for this purpose.

The following code can go between the <head> tags of any HTML or PHP page and Facebook will use it to populate the link preview with text and an image.

That means that now you can make an image specifically to viewed in a Facebook link preview.

<meta property="og:title" content="A title that will make readers curious" />
<meta property="og:description" content="Some detail about what is on the page." />
<meta property="og:image" content="image url" />

I’m Building A CSS Framework

By Adam Patel on May 13th, 2021

Before Makers, I wasn’t aware that CSS frameworks existed. I guess having learned from scratch and building my first website on Microsoft FrontPage 97, I learned CSS from first principles.

During Makers it became clear that I’d spent thousands of hours doing work that didn’t need to be done – it could have been done much faster and better if I’d just known what Bootstrap was. But then again, can you say that? Sometimes the goal you think you are chasing is just a vehicle by which you learn a skill. The actual project – like so many in my youth – never actually gets built. But I learn a lot along the way. And if it wasn’t for those thousands of hours I’d spent writing vanilla CSS, I don’t think I’d be in the position I’m in now where I can code a CSS layout from memory.

So, in recent weeks, I’ve noticed that I’ve been typing the same code over and over and over again when building new websites. There’s even a certain amount of code that I like to start with on any project that makes a few very important changes to the default settings imposed by browsers.

Then there’s the whole question of mobile layouts versus laptop layouts.

During Makers, I used Bootstrap on one project and Bulma on another. But in both cases, when you start to have preferences about your page design and don’t just want something respectable in the fastest possible time, you start to add custom CSS and, with both of those frameworks, we ended up with CSS conflicts that were too deep and complex for us to solve.

Ever since then, I’ve been thinking about creating my own CSS framework. Something highly flexible that doesn’t impose upon the designer a whole bunch of stuff they don’t want.

When you add bulma or bootstrap to a HTML or PHP file, you simply import it from a CDN (content delivery network) using one line of code in the HTML header. And that’s it. Sounds easy – but what if you don’t want EVERYTHING?

I want to take a different approach. I want to somehow create a CSS framework that allows the designer to take only what they want without any of the rest of it. That way, they’ll still be free to customise their own preferences without their code conflicting with the framework.

I have not yet fully decided how this will work, though I do have some initial ideas. It should be an interesting little project.

More when there is more.

The Truth About Search Volume Data & Market Demand

By Adam Patel on May 12th, 2021

Search volume tools have long been used as a means of gauging whether a product or service is in demand or not, but it’s only one way of doing it and it doesn’t always show you a true and accurate picture.

In this article, I explain three situations where the search volume data isn’t telling the truth about market demand.

Just because nobody searches for it, doesn’t mean nobody wants it…

It just means the people that do want it don’t know that they want it, or have a better way of finding it than typing it into Google.

For example, the term ‘javascript developer’ is searched only 140 times a month on average, here in the UK. But checkout LinkedIn and you’ll see that javascript developers are in high demand. But I can’t really see many hiring managers looking for a javascript developer by googling it. They instead put an advert on Indeed or LinkedIn and let javascript developers come to them.

And search volumes can be substantially lower than sales figures…

Meanwhile, a term like wedding dresses is searched approximately 49,500 times a month and yet I’m willing to bet there are far fewer weddings in the UK each month than that. It’s also the case that this figure is dramatically higher than the search volumes for other things and people you need on a wedding day, such as musicians, magicians and florists. Even something like a photographer – that is pretty much as quintessential as a wedding dress (if not more so, now that there are weddings where nobody wears a dress) – has a far lower monthly search figure than that of dresses.

So why the high search figure? Well… I have it on good authority that there is a demographic of women who like to look at wedding dresses even when they’re single. It’s just something they like to do.

What have we learned? That search volumes aren’t everything. They may be an indicator of interest in a subject or thing, but not necessarily.

Here’s a question…

What is the story behind the search query?

If we were to always take search volume data at face value, that would mean that all humans out there searching for things on Google were completely self-aware and knew exactly what they needed to solve their problems. Unfortunately, that isn’t true. And because of that, sometimes we need to read between the lines to see what’s really going on.

Suppose I’m selling an online course to help people find their dream jobs. But the term ‘how to find your dream job’ only gets 90 searches per month. Even if all 90 of them buy my course (highly unlikely), I’m still not going to do massively well out fo that.

It would be reasonable to assume that everybody would want their dream job and going by various statistics, it would be reasonable to assume that most people actually don’t have their dream job. So why the low search figure?

I’m willing to bet there’s a lot of murky thinking surrounding the issue. Many have jumped to the conclusion that they want to escape from working altogether. They’ve bought the ‘passive income’ lie that the Internet loves to peddle.

So let’s suppose somebody, Eric, searches for ‘invest in property’. I’m willing to bet that they don’t actually WANT to invest in property. Who would? It involves getting into debt. There’s risk involved. It could be very stressful. I’m willing to bet that most people looking to invest in property have a job they don’t like and they’re really looking to escape from that job. They have rationalised that if they had a property that a tenant was paying rent for, they wouldn’t have to do their crappy job anymore.

There will of course be people looking to invest in property for other reasons. But I believe in my theory that a proportion of people searching for that term are just looking to escape a day job they hate.

If my theories are true, then it is the case that the Eric is actually somebody I can help with my online course about finding your dream job. But rather than trying to figure out how to find a job he’s better suited for, Eric has decided to try and escape from work altogether by investing in property.

I want Eric to see my website. I think that if he does, he will like what he sees and at best, buy the course, and at worst, at least attend a free webinar.

How do I make my website visible to Eric, who doesn’t know that he’s looking for it?

Here’s my plan:

In this instance, I’m not immediately going to try and rank a page for the term ‘invest in property’. There are a number of reasons for this.

First of all, I’m quite sure there are in excess of 10 property ‘gurus’ out there who are selling week long seminars teaching people how to actually invest in property. They are charging thousands of pounds for their seminars so it’s going to make ‘invest in property’ a difficult term to rank for. There will almost definitely be a costly SEO campaign involved.

Secondly, I’m not sure my theory is true yet. What if I spent time and money ranking a page and then it turns out that my thinking, how ever logical it seemed, is just wrong.

So instead, I’m going to hit a bunch of the many property investment Facebook groups and I’m going to post genuine articles about ‘how to invest in property’. I have a targeted audience, so I’d expect a reasonable click-thru rate. On all of thse pages, I have Facebook Pixel setup.

This means that every time somebody clicks my link and lands on my page, they’re being added to a custom audience I’m created in my Facebook Ad Account. They don’t even have to read the article.

Next, I’m going to create an add titled something like ‘Escape The Day Job You Hate’ and I’m going to target my custom audience.

I will soon find out if my theory was correct. If it was, my ad will achieve a high click thru rate because I’m offering all the Erics an alternative method of getting what they really wanted in the first place. If I’m wrong, the ad will flop.

Hello world!

By Adam Patel on May 8th, 2021

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

‘If’ by Rudyard Kipling – One of My Favourite Poems

By Adam Patel on May 8th, 2021

man sitting on white concrete building during daytime

In life, sometimes, bad things happen. But whenever they do, I can usually find guidance in this poem…

The degree of wisdom in these lines in staggering.

If

If you can keep your head when all about you   
    Are losing theirs and blaming it on you,   
If you can trust yourself when all men doubt you,
    But make allowance for their doubting too;   
If you can wait and not be tired by waiting,
    Or being lied about, don’t deal in lies,
Or being hated, don’t give way to hating,
    And yet don’t look too good, nor talk too wise:

If you can dream—and not make dreams your master;   
    If you can think—and not make thoughts your aim;   
If you can meet with Triumph and Disaster
    And treat those two impostors just the same;   
If you can bear to hear the truth you’ve spoken
    Twisted by knaves to make a trap for fools,
Or watch the things you gave your life to, broken,
    And stoop and build ’em up with worn-out tools:

If you can make one heap of all your winnings
    And risk it on one turn of pitch-and-toss,
And lose, and start again at your beginnings
    And never breathe a word about your loss;
If you can force your heart and nerve and sinew
    To serve your turn long after they are gone,   
And so hold on when there is nothing in you
    Except the Will which says to them: ‘Hold on!’

If you can talk with crowds and keep your virtue,   
    Or walk with Kings—nor lose the common touch,
If neither foes nor loving friends can hurt you,
    If all men count with you, but none too much;
If you can fill the unforgiving minute
    With sixty seconds’ worth of distance run,   
Yours is the Earth and everything that’s in it,   
    And—which is more—you’ll be a Man, my son!