'Subtracting an interval from string (various units of time) using strtotime();
I am creating a ticket system. i have stored the interval between the tickets creation date & the current date.
Each ticket has a resolution time limit ($targetTime) stored as a string .. 15 minutes, 4 hours, etc.
I am trying to calculate the time remaining by subtracting the interval from the time limit string.
I have tried to use strtotime on $targetTime but I am struggling to understand how it should be formatted.
$then = new DateTime($query->fetchColumn());
$now = new DateTime();
$interval=date_diff($then, $now);
$targetTime = strtotime($targetTime);
$timeRemaining = $targetTime - $interval;
Solution 1:[1]
$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 ";
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arian |
