2012-03-31 86 views

回答

1

您可以爲您的腳本創建函數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($ timewritten)';

作用時間爲所有用戶..

  • 「會話」表中現在應該有「時區」一欄 - 讓用戶輸入自己的時區,VARCHAR(3)默認沒什麼
  • ,我們應該有時間區域的「選擇」選項!

//讓顯示器的實際使用時間的用戶..

//$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

//從數據庫中 - 即用於顯示評論或文章dateCreated會倍..

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

//顯示時鐘讓我們說..

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

感謝答案,我得到了點,確定的,如果任何一個在中國訪問我的網站,並根據時間差顯示只爲一個國家可能會再次出現問題,我覺得你得到了點,做我需要爲每個國家寫出差異?請代表 – 2012-04-02 05:24:49

+0

您必須允許(EVERY)用戶訪問您的網站以通過「選擇」下拉菜單輸入其時區。這包括你。我重新編輯了該範圍的功能。 – Xfile 2012-04-02 16:28:11

+0

好的,非常感謝 – 2012-04-03 06:42:12

0

以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; 
+0

感謝代表, – 2012-04-02 05:29:17

相關問題