Php local time by country code

Dear all, i am trying to find a way to get local time by country code.
If below example worked it had to show local time in Portugal.
Can you please give me a hint how to fix that?

<?php echo get_local_time('PT');?>

Timezones are not set with country code only. Take a look at php’s date_default_timezone_set() function and the list of supported time zones.

HI Frank, thank you. In my case the structure does not allow to make it work that way. Do you know if there is a way to get date and time by latitude and longitude?

This is and isn’t an easy question to answer. The easiest way to handle it, is to utilize a settings file for the user where they select their timezone. Any site that has time displays does this. The issue is, you can’t accurately gauge where a user is connecting from by other details.

If you don’t want to do that, the nearest best case scenario would be to do a reverse location lookup of their IP address, but that is not accurate. Sites that do this routinely are wrong because if I connect from my work network, the facing IP will be where ever the network people set that IP.

HI, thank you but my case is simple. I have webpages related to locations. For example page which is related to London. So i just want to show London local time on that page to the visitor who is browsing.

Well then you have to set date_default_timezone_set(‘Europe/London’); on the page about Londen. What is difficult about that?

Thank you. I am new. Well difficult is to find out how to show a date and a clock. That shows only the text Europe/London. I want to show date and hours in London.

echo date_default_timezone_get();

So you would set the default time zone, then give the current time.

$date = new DateTime();
echo $date->format('d/m/Y H:i:s');

Thank you all I am almost there. It works when i test it like this.

<?php  date_default_timezone_set("GB/London");
$date = new DateTime();
echo $date->format('d/m/Y H:i'); ?>

Then I replace ("GB/London") with <? php echo $ctr['Ctr']['Ctr'].'/'.$city['City']['City']?> which also takes value properly when i test it. But when i combine them both like this, the page breaks. It is probably syntax error. Would be thankful for a hint.

<?php  date_default_timezone_set("<? php echo $ctr['Ctr']['Ctr'].'/'.$city['City']['City']?>");
$date = new DateTime();
echo $date->format('d/m/Y H:i'); ?>

You are already in php, why are you adding php tags again?

Because i just entered a pre Beginner level (-: Thank you! Works like a charm now

Sponsor our Newsletter | Privacy Policy | Terms of Service