gpt4 book ai didi

PHP 和 MYSQL 日期错误

转载 作者:行者123 更新时间:2023-11-29 22:54:50 26 4
gpt4 key购买 nike

这是 strptime 到人类时间的函数...

PHP:

function humanTiming($tm,$rcs = 0)
{
$cur_tm = time(); $dif = $cur_tm-$tm;
$pds = array('second','minute','hour','day','week','month','year','decade');
$lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);

$no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]);
if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
return $x;
}

该函数工作得很好,但是当我传递 MySQL 时间戳时,它会出现错误并显示 40 年前的不切实际的日期......

如果我将 MySQL 时间戳发送到函数 ex:

2015-02-25 12:23:34

这是我遇到的错误,它返回这个

4 decades

最佳答案

使用 strtotime('2015-02-25 12:23:34') 应该可以解决问题。

strtotime 将字符串转换为 unix 时间戳,这在您的函数中是必需的。

如果您将字符串传递给该函数,例如 2015-02-25 12:23:34,它会将其转换为 0 作为 Unix 时间戳。自 1970 年 1 月 1 日起 0 秒,距今已有 4 多年历史。这里有一些关于此的文档 http://unixtimestamp.com

关于PHP 和 MYSQL 日期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28753955/

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