Problem:
You want to know the local date and time of your current location.
Solution:
One very important thing to consider before proceeding.
Beware of the server location
When you try to get the current time using PHP, by default PHP will return the local time of the server where it is located. So, if you’re from Rome Italy, but, your web server is located New York, US, then, PHP will return you the local time of the New York.
Change your timezone
So, to know the local time of your current location, change the timezone to match your present location. It’s very easy. Use date_default_timezone_set() function. Just mention right timezone as the parameter in that function. To find your timezone, see the list of supported Timezones.
The following code will show the current date and time of the Rome, Italy no matter where the web server is located.
<?php date_default_timezone_set("Europe/Rome"); echo "Current local date and time is ", date('d-m-Y H:i:s'); ?>
[wpdm_file id=114]
Output:
Current local date and time is 27-05-2014 08:42:16
How it works:
At first, we changed the timezone to Rome. Now, if we try to work with date time, the server will return date and time according to the local Rome time. You can adjust local time according to your timezone. Then, we used date() function which returns the date and time according to the format we specified in the parameter.