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.

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below. Unless you're opening a new php file :)

function cw_custom_greeting() {
	// Fallback time
	$time_gmt = new DateTimeZone("GMT");
	$time_amsterdam = new DateTimeZone("Europe/Amsterdam");
	$dateTime_gmt = new DateTime("now", $time_gmt);
	$timeOffset = $time_amsterdam->getOffset($dateTime_gmt);
	$timeOffset_hours = intval( $timeOffset ) / 3600;
	
	// Let's get the blog time of the user, the variables are named self-explanatory :)
	$user = wp_get_current_user();
	$user_id = $current_user->ID;
	
	$user_primary_blogid = get_user_meta($user_id, 'primary_blog', true);
	
	//Returns false if the user isn't logged in
	$blog_timeOffset = get_blog_option( $user_primary_blogid, 'gmt_offset' );

	// Take the blogtime, fallback to the Fallback time if false or not an integer.
	$timeOffset = is_int( $blog_timeOffset ) && $blog_timeOffset !== false ? $blog_timeOffset : $timeOffset_hours;
	
	// Convert to time in hours
	$time = gmdate("H", time() + 3600*($timezone+date("I")));
	
	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?';
	}
	
	// Output the greeting
	return esc_attr($greeting);
}

That’s it! Now where to put it?
For example’s sake, let’s change the admin footer text which says “Thank you for using WordPress.”.

// Filter the output with the function
add_filter('admin_footer_text', 'cw_change_footer_admin');

function cw_change_footer_admin() {
	// The time based greeting :)
	$greeting = cw_custom_greeting();
	
	echo $greeting;
}

Enjoy!

Comments, questions? Leave them below :)

Leave a Reply

Your email address will not be published. Required fields are marked *