PHP Date Generator

What is Date Generator?

Nice and simple one for this tutorial. We are going to create a php driven Date Generator like the one in the banner on this site. Don't worry, it is simple to do and adds an 'up-to-the-minute' feel to your web pages.

Show Us Then!

OK, all we are going to need for this is a nice little snippet of php code which extracts the date from the server where the website is hosted. Just be aware that it matters where your server is hosted. Corrosive Online uses UK based dedicated servers (who said British industry is dead?) so the caveat with Corrosive Online is to say that the date reflects the current date in the UK. All you need is this tiny piece of code to make it work;

<?php
$today = getdate();
?>

This code should sit on your page above the doctype. The code gets the date off of your server using the inbuilt 'getdate' PHP function. Once this is done you have your date details stored up in a PHP 'array' and ready for use.

The next step is to 'echo' the 'array' back out into HTML for the viewer and, crucially, in an order that looks good. For this you will need the following code where you want your date to appear;

<div class="date">Today's Date in yourcountry is: <?php echo("$today[weekday], $today[month] $today[mday], $today[year]");?></div>

I hope that you will notice how I have set up the date to read with Mon-Fri, then the month and the date and then the year. As long as you have local servers for your hosting then you can replace 'yourcountry' with where you are located. You will also need to style the 'date' class using CSS. Simple to do and it looks great on your web pages!

I do have one more trick up my sleeve with this one. It's always pretty sweet to have a copyright mark on your website but you often see '©2004' and similar on websites and I believe that looks really poor. It 'dates' a website design before you even look at the graphics and layout. Well, you can use the 'getdate' PHP function and 'echo' out just the year from the array and include this in your footer information. The code for this is;

Created by yourname &copy;<?php echo("$today[year]"); ?>

Replace 'yourname' with your name (we'll duh) and you are all set to go. When the clock strikes midnight on December 31st, where your servers are located, your copyright year will update without you having to change all your pages... or even lift a finger!

Advertisements If you would like to advertise on Corrosive Online then get in touch through my Contact Page for details.
Join The Mailing List
    If you have found anything on this site useful or entertaining or you have enjoyed one of my Online Web Design Lessons then I would love you to make a donation. Use the Paypal button below to make a donation.