PHP script/code to get Bangladeshi and any other local time
February 4, 2008
Often in local projects, the Bangladeshi developer may face problem in finding the local time since often the hosting is done in a server which is outside the country. Moreover, there are many different time formats like GMT, UTC etc. But a developer must use local time if it is included in the client’s requirements. Although a little bit browsing can help to solve the problem, I am sharing the solution with you so that you can re-use the code and save the browsing time. What is the benefit of re-inventing the wheel?
Now we can get the Bangladeshi time very easily if we add exactly only 6 (six) hours with GMT time. There may be other ways. But my solution solves it by adding 6 (six) hours with GMT time. Below is my simple function to get the Bangladeshi time in PHP.
<?php
function ShowBangladeshTime()
{
$hour = gmdate(“H”);
$minute = gmdate(“i”);
$seconds = gmdate(“s”);
$day = gmdate(“d”);
$month = gmdate(“m”);
$year = gmdate(“Y”);
// This is the offset from the server time to Bangladesh time.
$hour = $hour + 6;
return date(“h:i:s A Y-m-d”, mktime ($hour,$minute,$seconds,$month,$day,$year));
}
?>
You can also extract specific section of a time in this following way:
<?php
$dayOfTheWeek = date(“l”, mktime ($hour,$minute,$seconds,$month,$day,$year));
$hoursNow = date(“H”, mktime ($hour,$minute,$seconds,$month,$day,$year));
$minutesNow = date(“i”, mktime ($hour,$minute,$seconds,$month,$day,$year));
$AmPmNow = date(“A”, mktime ($hour,$minute,$seconds,$month,$day,$year));
?>
If you have to play with multiple country times, you can just add a switch case way like this:
<?php
function ShowSelectedCountryTime($country)
{
$hour = gmdate(“H”);
$minute = gmdate(“i”);
$seconds = gmdate(“s”);
$day = gmdate(“d”);
$month = gmdate(“m”);
$year = gmdate(“Y”);
switch($country)
{
case “Bangladesh”:
$hour = $hour + 6;
break;
case “India”:
$hour = $hour + 5;
$minute = $minute + 30;
break;
case “Nepal”:
$hour = $hour + 5;
$minute = $minute + 45;
break;
default:
$hour = $hour + 6;
}
return date(“h:i:s A Y-m-d”, mktime ($hour,$minute,$seconds,$month,$day,$year));
}
?>
It is simple to integrate such kind of simple function, script or code in your PHP class according to your requirements.
You can also download the PDF version of this article here below:
PHP script/code to get Bangladeshi and any other local time
May 4, 2008 at 9:30 pm
Thanks for this nice post. It’s really useful.
But u can minimize ur code a lot.
The following line is enough to get Bangladesh time as timestamp:
strtotime(‘+6 hours’, strtotime(gmdate(“M d Y H:i:s”)));
Now you can format the timestamp as u like by date() function. The technique can be used to get any local time.
May 5, 2008 at 5:20 am
Hello Anis,
You are right.
But if you think of the global perspective, you may need to add or subtract hours, minutes and even days to get local time.
I was thinking globally.
There are around 200 or more countries.
In my coding, I tried to cover any local time.
My other purpose was to write code that is very easy to understand.
So, I wrote everything in a user-friendly manner.
Code minimization was not my purpose.
My purpose was to write global code that is re-usable by every user and very easy & clear to understand.
May 5, 2008 at 10:05 am
Hi friend,
You can get any local time by this code, no problem
I’ve used ’+6 hours’ here for Bangladesh time.
You can “add or subtract hours, minutes and even days” here. For example, just use ‘+5 hours 30 minute’ for India time.
May 5, 2008 at 11:15 am
Hello Anis,
I know we can do that.
But I tried to cover the novice to pros so that everyone can understand from the detailed code.
I always prefer to write clean, easily-understandable and smooth performing code.
So, I am ready to compromise with a few more lines.