I have two Zend Date objects to work with:
$now = Zend_Date::now();
$sessionStart = new Zend_Date('2010-04-01 10:00:00', 'yyyy-MM-dd HH:mm:ss');
I need to calculate the time remaining and display in a human readable manner.
The session will begin in 7 days.
The session will begin in 30 minutes.
How can I do this with PHP and the Zend Framework?
Solution
Here's what I came up with. If you've got a better solution, please post it.
$remainingSeconds = $sessionStart->getTimestamp() - $now->getTimestamp();
// or: $remainingSeconds = $sessionStart->sub($now);
$remainingMinutes = $remainingSeconds / 60;
if ($remainingMinutes < 0) {
//session is in the past
return '0 days';
}
if (floor($remainingMinutes) <= 1) {
return '1 minute';
}
if (floor($remainingMinutes) < 60) {
return floor($remainingMinutes) . ' minutes';
}
$remainingHours = $remainingMinutes / 60;
if (floor($remainingHours) <= 1) {
return '1 hours';
}
if (floor($remainingHours) < 24) {
return floor($remainingHours) . ' hours';
}
$remainingDays = $remainingHours / 24;
if (floor($remainingDays) <= 1) {
return '1 day';
}
return floor($remainingDays) . ' days';