Author: Sazzad Hossain
Language: PHP (basic level)
Summary: As you may have seen on Google, the layouts change by time. If it's morning, a bright image displaying the morning scenery appears, the same goes for afternoon and night. This looks something hard to make right? Well it's really not. This function can easily be obtained by gathering the time and giving a load function for each time. In this tutorial I will teach you how to have a different image for each month of the year without the use of any database.
Before we continue, I want you to know that you can use this tutorial for displaying images for any hour, day, month, or even year. I use this to show the weather condition of where I live depending on the month.
Step 1: The File
There are two ways to do this. You can either have this function load from an external location or paste the code inside a current file. I would recommend that we have this on an external location to make our life a lot easier. We can accomplish this by creating a file taking advantage of php's include function.
So let's create a blank file called monthly.php and open it using your favorite code editor.
Step 2: Loading the Time/Month
To many people this step may seem the hardest since they really don't know how to accomplish it. Well I'll show you how using a very simple, quick, one-line code. The secret is a built in function of php that allows us to load the server time. This function is called the date function.
<?php
$month=date('m');$month is the name we are going to give to our command. You can change this to anything (ex: $day, $year, $today, etc.).
date() is a built-in php function that loads the current time depending on the variable within the parenthesis ().
m is the time variable I am telling the date function to load. This "m" will load the month in the form of 01, 02... instead of January, February... You can find the complete list of date functions in my explanation of php's Date function.
Step 3: Monthly Images
Here is where we will do a lot copy and pasting. This is the portion where we give each month a value. So we will include all our images here. Let's start of the first month of the year: January. We can achieve this through the use of if and else statements.
if($month=='01'){
echo "<img src='images/january.png' /><br />January";
}This portion is pretty self explanatory. On the first line of code basically what we say is that if the month is 01 (January) then do the following. Then on the next line of code we just paste the message within the single quotes using the echo function. Do note how I used single quotes (') instead of double quotes ("). This prevents the server in thinking that we completed the statement.
For every other month, we will use the else function. It's fairly the same as the statement above, just going replace if with else.
if($month=='02'){
echo "<img src='images/february.png' /><br />February";
}Repeat the code above for each month of the year. Do remember that in order for your html to be valid, you will need to give the image an alternative name. In addition, you have to remember that at the end you will of your code you will need to end the php callout. You can accomplish this by adding the following line at the very end.
?>
You can include this file on any page using the following code
<?php include("monthly.php"); ?>Additional Notes: In the codes above, I put the date in manually. You can have the server input the date for you so that you don't have to do much work. This can be accomplished with the following
else($month==02){
echo "<img src='images/february.png' /><br />" .$month;
}In a couple of my project I have changed the function to echo a class. So instead of this code
echo "<images/march.png' /><br />March"
I tend to use the following
echo "march"
So that when I want the header to change for every month I have it load like so
<div class="header_<?php include('monthly.php'); ?>">Be creative and play around with this.
Help




















