日期/时间转换为自PHP以来的时间
发布时间:2020-12-13 21:37:20 所属栏目:PHP教程 来源:网络整理
导读:我的数据库日期/时间的Hello是这种格式 2010-06-01T18:20:25+0000 我想回应那个日期/时间以来的时间 例如 4天3小时36分4秒 这可能吗? 解决方法 下面是我写的一个函数.随意使用它. /** * Returns rough (in largest single unit) time elapsed between two t
我的数据库日期/时间的Hello是这种格式
2010-06-01T18:20:25+0000 我想回应那个日期/时间以来的时间 例如 4天3小时36分4秒 这可能吗? 解决方法
下面是我写的一个函数.随意使用它.
/** * Returns rough (in largest single unit) time elapsed between two times. * @param int $iTime0 Initial time,as time_t. * @param int $iTime1 Final time,as time_t. 0=use current time. * @return string Time elapsed,like "5 minutes" or "3 days" or "1 month". * You might print "ago" after this return if $iTime1 is now. * @author Dan Kamins - dos at axonchisel dot net */ function ax_getRoughTimeElapsedAsText($iTime0,$iTime1 = 0) { if ($iTime1 == 0) { $iTime1 = time(); } $iTimeElapsed = $iTime1 - $iTime0; if ($iTimeElapsed < (60)) { $iNum = intval($iTimeElapsed); $sUnit = "second"; } else if ($iTimeElapsed < (60*60)) { $iNum = intval($iTimeElapsed / 60); $sUnit = "minute"; } else if ($iTimeElapsed < (24*60*60)) { $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour"; } else if ($iTimeElapsed < (30*24*60*60)) { $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day"; } else if ($iTimeElapsed < (365*24*60*60)) { $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month"; } else { $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year"; } return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : ""); } 要使用此func,您需要先将时间转换为time_t格式(自“epoch”以来整数#seconds).这些PHP函数中的任何一个都可能有助于:http://php.net/strptime或http://php.net/strtotime. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |