2015-05-03 90 views
2

由于我使用不同的时区(开罗),我联系了我在Godaddy的共享主机的托管团队来更改服务器时间,但他们无法在共享主机上更改它。根据时区计算数据输入数据库的时间

我希望你能帮我修改我用来计算用户插入数据到Mysql数据库以来的时间的代码。 我使用datetime作为日期字段。

下面是代码:

//This is the date the code inserts to the database: 

$date = new DateTime("now", new DateTimeZone('Africa/Cairo')); 
$date=$date->format("Y-m-d H:i:s"); 

//And this is the php function that calculates the time since post published: 
function humanTiming($time) 
{ 

$time = time() - $time; // to get the time since that moment 

$tokens = array (
    31536000 => ' year', 
    2592000 => ' month', 
    604800 => ' week', 
    86400 => ' day', 
    3600 => ' hour', 
    60 => ' minute', 
    1 => ' second' 
); 

foreach ($tokens as $unit => $text) { 
    if ($time < $unit) continue; 
    $numberOfUnits = floor($time/$unit); 
    return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'':''); 
} 

} 

回答

1

而不是使用您的真实时区“非洲/开罗,您可以使用服务器的时区(以匹配数据库)。既然你正在寻找时差,那就很准确。

+0

问题是,我还使用文章内的日期详细信息(例如2015年5月1日12:12:18),所以我需要两者,插入数据的时间以及用户发布文章的准确日期时间。 – weblover

+0

问题:“用户发布他的文章的准确日期时间”的参考点是什么?那是根据出版商或开罗的时区? – olkid

+0

参考是开罗时间。 – weblover