0

I made a project with PHP that was expected to run inside my country Iran that its timezone is UTC+3:30. I inserted the line below inside my codes:

date_default_timezone_set('Asia/Tehran');

But now, I think it may be used by others from different countries. As a part if this project contains users signin logs, I must set the time matual with their country timezones.
How can I do it?

Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127
  • 1
    There is too little information to answer this question. Any application that's timezone-aware would need to (a) know each user's timezone and (b) do all time calculations appropriately to account for the difference between the user and server timezones. – Jon Oct 30 '12 at 12:55
  • It will pay to set it to one timezone and go by that.. – Chris Oct 30 '12 at 12:56
  • 1
    my practice is to set a GMT across your project and set every user a timezone. So all users will see their datetime according to their own time zones. – Arfeen Oct 30 '12 at 12:59
  • 2
    @Arfeen's approach is the best: use the standard UCT timezone for all internal code and only convert it to local time when for user input/output. This makes things *so* much easier when you need to start dealing with users in multiple timezone, and it also avoids all the issues with timezones that have DST (not sure if that affects you locally, but it may affect your users elsewhere). (note to Arfeen -- it's only called GMT if it's referring to the UK's local timezone; when used a universal point of reference, it's called UCT. GMT and UCT are basically the same though) – SDC Oct 30 '12 at 13:19
  • @Arfeen, thanks for your valuable answer. Do you know a source that I can find a list of countries and their UTC values? – Mohammad Saberi Oct 30 '12 at 13:27
  • @SDC can you tell me what are the best functions to store default UTC time and convert it to specific timezone? – Mohammad Saberi Oct 30 '12 at 13:32
  • @SDC Ahh yes. Thanks for the correction. I should call that UCT. – Arfeen Oct 30 '12 at 13:39
  • @MohammadSaberi http://timezonedb.com/download have something for you and if you google you can find lot more. – Arfeen Oct 30 '12 at 13:39

2 Answers2

0

Here is how you can set the timezone:

<?php
session_start();
if(!isset($_SESSION['timezone']))
{
    if(!isset($_REQUEST['offset']))
    {
    ?>
    <script>
    var d = new Date()
    var offset= -d.getTimezoneOffset()/60;
    location.href = "<?php echo $_SERVER['PHP_SELF']; ?>?offset="+offset;
    </script>
    <?php    
    }
    else
    {
        $zonelist = array('Kwajalein' => -12.00, 'Pacific/Midway' => -11.00, 'Pacific/Honolulu' => -10.00, 'America/Anchorage' => -9.00, 'America/Los_Angeles' => -8.00, 'America/Denver' => -7.00, 'America/Tegucigalpa' => -6.00, 'America/New_York' => -5.00, 'America/Caracas' => -4.30, 'America/Halifax' => -4.00, 'America/St_Johns' => -3.30, 'America/Argentina/Buenos_Aires' => -3.00, 'America/Sao_Paulo' => -3.00, 'Atlantic/South_Georgia' => -2.00, 'Atlantic/Azores' => -1.00, 'Europe/Dublin' => 0, 'Europe/Belgrade' => 1.00, 'Europe/Minsk' => 2.00, 'Asia/Kuwait' => 3.00, 'Asia/Tehran' => 3.30, 'Asia/Muscat' => 4.00, 'Asia/Yekaterinburg' => 5.00, 'Asia/Kolkata' => 5.30, 'Asia/Katmandu' => 5.45, 'Asia/Dhaka' => 6.00, 'Asia/Rangoon' => 6.30, 'Asia/Krasnoyarsk' => 7.00, 'Asia/Brunei' => 8.00, 'Asia/Seoul' => 9.00, 'Australia/Darwin' => 9.30, 'Australia/Canberra' => 10.00, 'Asia/Magadan' => 11.00, 'Pacific/Fiji' => 12.00, 'Pacific/Tongatapu' => 13.00);
        $index = array_keys($zonelist, $_REQUEST['offset']);
        $_SESSION['timezone'] = $index[0];
    }
}
date_default_timezone_set($_SESSION['timezone']);

//rest of your code goes here
?>

Source

But I recommend not to do that. It would be better to set GMT (or your timezone) as default and change the information from the logs according the current timezone of each user when it is displayed.

enenen
  • 1,967
  • 2
  • 17
  • 33
0

Try this:

<?php
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
echo $fmt->format(time());
?>
Shafizadeh
  • 9,960
  • 12
  • 52
  • 89