Greeting upon user’s blog Timezone

Sybre Waaijer on July 7, 2015

About PHP

So you want to show a greeting upon the time of your blog? That’s easy!
Well, not really. Took me 5 different php.net pages and examples to get to where I wanted. The reason because of this is that the PHP.net manual isn’t always super efficient in explaining their functions.

Especially when it comes to something complex as Daylight Savings Time. Remember the fuss around the year 1999 to 2000?

Anyway, let’s cut to the chase.

Pure PHP

First, let’s do this without any options. This mean this will work without the help from WordPress.

As you’ll notice, it’s using PHP objects, rather than functions. We assume you live in the Amsterdam Timezone.

For a complete list of timezones, go and select your base here: PHP Timezones.
E.g. if you live in Currie Australia, replace “Europe/Amsterdam” with “Australia/Currie”.

//Get the GMT timezone
$time_gmt = new DateTimeZone("GMT");
//Get the Amsterdam timezone
$time_amsterdam = new DateTimeZone("Europe/Amsterdam");
//Get the time from GMT
$dateTime_gmt = new DateTime("now", $time_gmt);
//Get the offset from Amsterdam to GMT in seconds
$timeOffset = $time_amsterdam->getOffset($dateTime_gmt);
// Transform the difference in seconds to hours
$timeOffset_hours = intval( $timeOffset ) / 3600;
// Get the current GMT time in hours and add the difference in hours consulting the DST.
// The "I" becomes either 1 or 0 depending on daylight saving (1 in summer, 0 in winter).
// The outcome is the current time in hours. When this time exceed 23 hours it will start back at 0.
$time = gmdate("H", time() + 3600*($timeOffset_hours+date("I")));
/* We're going to use this $time variable in the next snippet */

Ok, so now we know what time it is in hours. Great! Now let’s add greetings.
We’ll call them: “Good morning”, “Good afternoon”, “Good Evening” and “What are you still doing up?”.

// Let's count down and start with above 18 hours. AKA evening.
// If the first if statement is false, it will try the next.
// So counting up would be a problem because it will always be true.
if ($time >= '18' ) {
$greeting = 'Good Evening';
} else if ($time >= '12' ) {
$greeting = 'Good afternoon';
} else if ($time >= '6' ) {
$greeting = 'Good morning';
} else {
$greeting = 'What are you still doing up?';
}
// Place it somewhere :D
echo $greeting;

Great! That’s pretty much it. Put it in a function or where ever you wish :) It’s yours!

WordPress Blog Integration

WordPress has great features, and one of the best is time… because then you might be able to know when you’ve got hacked on your outdated installation. Go update. Do it. Now.

So when you’ve updated your WordPress to the XSS safe version, it’s now time to copy and paste this code somewhere. I’ve explained most of it in the code snippets above.
But now we also want to add the user’s blog time.

The problem we’re going to face is that the blogtime can be set to either a timezone by city which will take Daylight Savings into account, or UTC+ time which doesn’t take Daylight Saving into account.