使用strtotime();从字符串(各种时间单位)中减去间隔



我正在创建一个票证系统。我已经存储了票据创建日期&当前日期。

每个票证都有一个以字符串形式存储的解析时间限制($targetTime(。。15分钟、4小时等

我正试图通过从时间限制字符串中减去间隔来计算剩余时间。

我曾尝试在$targetTime上使用strtotime,但我很难理解它应该如何格式化。

$then = new DateTime($query->fetchColumn());
$now = new DateTime();
$interval=date_diff($then, $now);
$targetTime = strtotime($targetTime);
$timeRemaining = $targetTime - $interval;
$targetTime = 1649991600;
echo timeLeft($targetTime);
function timeLeft($timestamp) {
$strTime = array("second", "minute", "hour", "day", "month", "year");
$length = array("60","60","24","30","12","10");
$currentTime = time();
if($currentTime <= $timestamp) {
$diff     = $timestamp-time();
for($i = 0; $diff >= $length[$i] && $i < count($length)-1; $i++) {
$diff = $diff / $length[$i];
}

$diff = round($diff);
if ($diff == 1) {
return $diff . " " . $strTime[$i] . " left ";
}else{
return $diff . " " . $strTime[$i] . "s left ";
}
}
}

相关内容

  • 没有找到相关文章

最新更新