Php 如何根据国家时区显示时间
当我访问我在美国托管的网站并从巴基斯坦查看时,它实际上会向我显示美国的时间和日期,您可以使用函数设置时区。您可以为脚本创建函数DisplayDate($datetoconvert),将时间转换为您的时间,并返回新时间Php 如何根据国家时区显示时间,php,time,Php,Time,当我访问我在美国托管的网站并从巴基斯坦查看时,它实际上会向我显示美国的时间和日期,您可以使用函数设置时区。您可以为脚本创建函数DisplayDate($datetoconvert),将时间转换为您的时间,并返回新时间 DisplayDate($xtime) //assuming its a unix timestamp.. { $mydifferece = 6; // hours $newtime = $xtime+($mydifference*3600); // $newtime = dat
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($timewrited)。'
所有用户的功能时间..
- “session”表现在应该有“timezone”列-用户可以输入他们的时区,varchar(3)默认为nothing
- 我们应该有时区的“选择”选项李>
//$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;
谢谢你的回答,我明白了,好吧,就一个国家而言,如果中国有人访问我的网站,并且根据差异显示时间,问题可能会再次出现,我想你明白了,我需要为每个国家写差异吗???请注意,您必须允许您网站的(每个)用户通过“选择”下拉菜单输入其时区。这也包括你。我为这个范围重新编辑了一点函数。