eRott Posted May 24, 2009 Share Posted May 24, 2009 Hey, new problem I could use some help with. What I am looking to do is obtain the remaining time until a future event occurs. Say for example, I have a lunch meeting on May 26 @ 12:00 PM and today's date is May 24 @ 2:00 PM. I want to calculate and display the remaining time until that event is scheduled to take place; which is 2 days and 2 hours (I think :-\). As it stands I have the event date stored as a UNIX timestamp in a database. I was experimenting and came up with this: index.php $finalTime = timeLeft(date("H", $event['start']), date("i", $event['start']), date("s", $event['start']), date("n", $event['start']), date("j", $event['start']), date("Y", $event['start'])); $remainingTime = explode(',', $finalTime); echo "Hours => ".$remainingTime[0]."<br />"; echo "Minutes => ".$remainingTime[1]."<br />"; echo "Seoonds => ".$remainingTime[2]."<br />"; echo "Months => ".$remainingTime[3]."<br />"; echo "Days => ".$remainingTime[4]."<br />"; echo "Years => ".$remainingTime[5]; timeLeft() function timeLeft($hours, $minutes, $seconds, $months, $days, $years) { // mktime(hour, minute, second, month, day, year) // mktime (H, i, s, n, j, Y) // mktime(23, 00, 00, 5, 23, 2009) // H = 24-hour format of an hour with leading zeros // i = Minutes with leading zeros // s = Seconds, with leading zeros // n = Numeric representation of a month, without leading zeros // j = Day of the month without leading zeros // Y = A full numeric representation of a year, 4 digits $totalHours = $hours - date("H"); $totalMinutes = $minutes - date("i"); $totalSeconds = $seconds - date("s"); $totalMonths = $months - date("n"); $totalDays = $days - date("j"); $totalYears = $years - date("Y"); $finalTime = $totalHours.",".$totalMinutes.",".$totalSeconds.",".$totalMonths.",".$totalDays.",".$totalYears; return $finalTime; } Its very basic and flawed as it will work only under a very strict set of circumstances. Notably: [*]The event day is the same as the current day [*]The event hour is greater then the current hour [*]The event minute is greater then the current minute So obviously it isn't going to work. Which is where you, the PHPFreaks community, steps in yet again to hopefully shed some light on this problem. With that said, are there PHP functions already in existence to accomplish this? Do you know of any resources I could read? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/159440-solved-calculate-remaining-time/ Share on other sites More sharing options...
Andy-H Posted May 24, 2009 Share Posted May 24, 2009 function eventTime($timestamp) { $time = $timestamp - time(); $secs = $time; $mins = 0; $hrs = 0; $days = 0; while ($secs >= 60) { $mins++; $secs -= 60; } while ($mins >= 60) { $hrs++; $mins -= 60; } while ($hrs >= 24) { $days++; $hrs -= 24; } $str = "<p>"; if ( isSet($days) ) $str.= ($days > 1) ? $days . " days, " : $days . " day, "; if ( isSet($hrs) ) $str.= ($hrs > 1) ? $hrs . " hours, " : $hrs . " hour, "; if ( isSet($mins) ) $str.= ($mins > 1) ? $mins . " minutes " : $mins . " minute "; if ( isSet($secs) ) $str.= ($secs > 1) ? "and " . $secs . " seconds" : "and " . $secs . "second"; $str.= "</p>"; return $str; } Quote Link to comment https://forums.phpfreaks.com/topic/159440-solved-calculate-remaining-time/#findComment-841062 Share on other sites More sharing options...
eRott Posted May 24, 2009 Author Share Posted May 24, 2009 Ohhhh okay. I see what you've done to accomplish it. Suddenly, this task didn't seem so hard in the first place. You simply subtract the current timestamp from the event timestamp to get the number of seconds remaining (since timestamps are just the number of seconds since 01/01/1970). Then you up each unit of time by 1 accordingly based on the number of seconds until that unit has reached its maximum at which point it ups the next unit of time by 1. Although, I am unsure exactly what these little tidbits do: $secs -= 60; After checking, the -= operand is the equivalent of saying x=x-y. Wait a minute, alright, I understand it. So that line is basically saying: The number of seconds = the number of seconds - 60 but only when the maximum value of that unit has been reached and the next unit of time is upped by one. So in other words, once the minutes count has reached 60, you up the hours by one and reset the minutes count to 0. Same thing applies to the hours except the count is 24, not 60. Alright, I'm fairly confident I understand how this function works now. Thank you kindly for your assistance. Take it easy. Quote Link to comment https://forums.phpfreaks.com/topic/159440-solved-calculate-remaining-time/#findComment-841190 Share on other sites More sharing options...
Andy-H Posted May 25, 2009 Share Posted May 25, 2009 Thats the jist of it Quote Link to comment https://forums.phpfreaks.com/topic/159440-solved-calculate-remaining-time/#findComment-841536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.