PHP: A Simple Function For Getting The Current Nigeria Local Time

If you ever noticed, the PHP date() function returns an incorrect time, if you’re not in the same country as your server. The date () function has been built to return only the date/time of the server and not the date/time of the country you are. I wrote this script to get the current local time in Nigeria alone, you can also further modify it to your taste, or perhaps change the time zone in date_default_timezone_set(‘Africa/Lagos’); to your timezone( the timezone of your country, if you’re not in Nigeria but also not getting a correct time when you call the date() function).

< ?php
/*
 * @author: Aly Chiman
 * @package: Function for getting Nigeria Local time 
 * @URI:  http://www.alychitech.com
 */

function get_ng_time() {

date_default_timezone_set('Africa/Lagos');

<pre><code>$time = date('h:i A');

return $time;
</code></pre>

}
?>

How it works

Thedate_default_timezone_set(‘Africa/Lagos’); sets the default timezone to Africa/Lagos which happens to be Nigeria timezone. The $time variable stores the time format which the function returns each time the function is being called. We can further customize it to echo a greeting message alongside the time

&lt; ?php
/*
 * @author: Aly Chiman
 * @package: Function for getting Nigeria Local Time
 * @URI:  http://www.alychitech.com
 */

function get_ng_time() {

<pre><code>date_default_timezone_set('Africa/Lagos');

$time = date('h:i A');
$message = 'Hello, welcome to my blog. It is '.$time.' here in Nigeria.';
return $message;
</code></pre>

}
?>

You can replace the message in the $message variable with a welcome message that you’d prefer; and you can simply call the function and echo the current time in Nigeria with echo get_ng_time(); .

Hope this helps, Cheers!