PHP Quick Script: Displaying copyright year dynamically
Wouldn’t it be nice to never have to change your copyright year again. I was growing tired of changing my copyright year every year on each and every website, granted, it wasn’t that much work because the footer was usually included into the page, but still, with this tiny time script you’ll never have to do it again.
So let’s see this script shall we…
[sourcecode language="PHP"]
// GMT time zone hour (do NOT insert + or -) available at
// http://wwp.greenwichmeantime.com/
$gmt = 2;
// Multiply the gmt hour by 60
$hour_min = $gmt*60;
// Now multiply the result with 60
$min_sec = $hour_min*60;
// Tell the gmdate function which date & time paramaters
// must be shown. Add or subtract the amount of seconds
// to standard GMT time to get the correct time.
$date_time = gmdate("Y", time()+$min_sec);
// Echo the current date and time
echo $date_time;
[/sourcecode]
But that’s not a particularly tiny script and it takes a few steps to get the same result we would get when calculating the seconds beforehand so let’s get the seconds.
South Africa has a GMT (Greenwich Mean Time) of + 2 hours, converting that to seconds is easy. Multiply the 2 with 60 and multiply the result by 60 and there you have it, the GMT of your country in seconds. + 2 hours will equate to 7200 seconds so we can use a much shorter version of the script…
[sourcecode language="PHP"]
echo gmdate("Y", time()+7200);
[/sourcecode]
Using this little piece of code will update the year, every year without you having to touch it. It’s a lot more efficient than changing everything by hand allowing you to spend more time browsing our site.
