如何根据国家时区显示时间


How to display time according to country timezone

当我访问我在美国托管的网站时,我从巴基斯坦观看它,它实际上显示了我在美国的时间和日期,

您可以使用date-default-timezone-set()函数设置时区。

您可以为脚本创建函数DisplayDate($datetoconvert),将时间转换为您的时间,并返回新的时间。

DisplayDate($xtime) //assuming its a unix timestamp..
{
$mydifferece = 6; // hours
$newtime = $xtime+($mydifference*3600); //
$newtime = date("d-m-Y H:i:s", $newtime);
return $newtime;
}

//html

编写者:'.DisplayDate($timewritted).';

所有用户的功能时间

  • "session"表现在应该有"timezone"列-对于用户输入他们的时区,varchar(3)默认为none
  • 我们应该有带时区的"选择"选项

//允许向用户显示实际时间。。

//$xtime is database entry to be converted (if any)
//$usertimezone is a selected timezone by user, driven from session (guests) - or users table.. (ie +6, or -7, or +1 and so on..)

       DisplayDate($xtime="",$usertimezone="") 
        {
          if(!$xtime) //given time (to be converted) dont exists, okay - lets give some right now time..
          {
          $xtime = time(); //set time to "right now", unix timestamp
          }
         //now we have time, lets see if user selected his timezone..
         if($usertimezone != "") //someone selected his timezone, time to do the job..
         {  
//herein (additionally) we should add support for winter and summer time (daylight) - if that zone support that changes.. but im leaving that empty for now..
         $xtime = $xtime+($usertimezone*3600); //difference * seconds per hour
         }
         else
         {
         //usertimezone not set, user dont care about his time to be displayed correctly, or its just a bot or spider, so do nothing..
         }
         $xtime = date("j. m, Y. H:i:s", $xtime); //lets just do some cosmetics..
         return $xtime;
    }

//HTML

//来自数据库-即用于显示评论或文章日期创建时间。。

echo DisplayDate($comment['commentdate'],$session['usertimezone']);

//显示时钟,比方说。。

echo DisplayDate('',$session['usertimezone']);

将日期存储在UTC时区中,将用户的时区首选项存储在其配置文件中,然后将日期显示在用户的首选时区中。

<?php
// assume these preferences came from a database as-is
$userTimezonePreference = 'America/Denver';
$storedDateTimeAsUtc    = '2012-04-02 06:15:40';
$dateTime               = new DateTime($storedDateTimeAsUtc, new DateTimeZone('UTC'));
echo 'ORIG: ', $dateTime->format(DateTime::RFC2822), PHP_EOL;
$dateTime->setTimezone(new DateTimeZone($userTimezonePreference));
echo 'USER: ', $dateTime->format(DateTime::RFC2822), PHP_EOL;