gpt4 book ai didi

php - 根据时区计算自数据进入数据库以来的时间

转载 作者:行者123 更新时间:2023-11-29 02:56:44 24 4
gpt4 key购买 nike

我联系了我在 Godaddy 的共享主机的托管团队以更改服务器时间,因为我使用不同的时区(开罗),但他们无法在共享主机上更改它。

我希望你能帮我修改我用来计算自用户将数据插入Mysql数据库以来的时间的代码。我将日期时间用于日期字段。

代码如下:

//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)?'':'');
}

}

最佳答案

您可以使用服务器的时区(以匹配数据库),而不是使用您的真实时区“非洲/开罗”。由于您正在寻找时差,因此它是准确的。

关于php - 根据时区计算自数据进入数据库以来的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com