icez Posted September 1, 2008 Share Posted September 1, 2008 No idea how to do that, I want to know the time elasped between my mysql data ((time) $now = date("H:i:s"); ) to now. Example will be easier to understand Mysql data - Now = Time elapsed Link to comment https://forums.phpfreaks.com/topic/122222-time-elapsed/ Share on other sites More sharing options...
JasonLewis Posted September 1, 2008 Share Posted September 1, 2008 Um, I usually do these sorts of things the hard way. Here is one option: <?php $sqlTime = "29th August 2008"; $sqlTime = strtotime($sqlTime); //Convert the SQL time to a Unix Timestamp $curTime = time(); //Get the current Unix Timestamp. $elapsed = $curTime - $sqlTime; //Get the current Unix Timestamp $days = 0; $hours = 0; $minutes = 0; while($elapsed > 86400){ $elapsed -= 86400; $days += 1; } while($elapsed > 3600){ $elapsed -= 3600; $hours += 1; } while($elapsed > 60){ $elapsed -= 60; $minutes += 1; } echo $days . " days, " . $hours . " hours and " . $minutes . " minutes have passed."; //of course you can use $elapsed, because the remaining number in $elapsed will be the seconds ?> Link to comment https://forums.phpfreaks.com/topic/122222-time-elapsed/#findComment-631055 Share on other sites More sharing options...
icez Posted September 1, 2008 Author Share Posted September 1, 2008 Thank a lot!! Link to comment https://forums.phpfreaks.com/topic/122222-time-elapsed/#findComment-631064 Share on other sites More sharing options...
thebadbad Posted September 1, 2008 Share Posted September 1, 2008 I once wrote a function using gmdate(). Check it out if you like: <?php // This function returns an array('year(s)' => $years, 'months' => $months, 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds) // If parameters contain only digits, they are treated as Unix timestamps, else they are converted using strtotime() // If no $endTime is provided, the function uses the current time // Fails on timestamps prior to ~ 1970 due to Unix timestamp limitations on PHP versions prior to 5.1.0 function timeDifference($startTime, $endTime = false) { $startTime = ctype_digit($startTime) ? $startTime : strtotime($startTime); $endTime = $endTime ? (ctype_digit($endTime) ? $endTime : strtotime($endTime)) : time(); if ($endTime > $startTime) { $diff = $endTime - $startTime; $y = gmdate('Y', $diff) - 1970; $mo = gmdate('n', $diff) - 1; $d = gmdate('j', $diff) - 1; $h = gmdate('G', $diff); $mi = ltrim(gmdate('i', $diff), '0'); $s = ltrim(gmdate('s', $diff), '0'); return array( ($y == 1) ? 'year' : 'years' => $y, ($mo == 1) ? 'month' : 'months' => $mo, ($d == 1) ? 'day' : 'days' => $d, ($h == 1) ? 'hour' : 'hours' => $h, ($mi == 1) ? 'minute' : 'minutes' => $mi, ($s == 1) ? 'second' : 'seconds' => $s ); } else { die('endTime parameter must be larger than startTime parameter in timeDifference function.'); } } // Sample usage $array = timeDifference('2008-08-20 12:00:00'); foreach($array as $unit => $value) { echo $value, ' ', $unit, '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/122222-time-elapsed/#findComment-631074 Share on other sites More sharing options...
icez Posted September 1, 2008 Author Share Posted September 1, 2008 Quote I once wrote a function using gmdate(). Check it out if you like: <?php // This function returns an array('year(s)' => $years, 'months' => $months, 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds) // If parameters contain only digits, they are treated as Unix timestamps, else they are converted using strtotime() // If no $endTime is provided, the function uses the current time // Fails on timestamps prior to ~ 1970 due to Unix timestamp limitations on PHP versions prior to 5.1.0 function timeDifference($startTime, $endTime = false) { $startTime = ctype_digit($startTime) ? $startTime : strtotime($startTime); $endTime = $endTime ? (ctype_digit($endTime) ? $endTime : strtotime($endTime)) : time(); if ($endTime > $startTime) { $diff = $endTime - $startTime; $y = gmdate('Y', $diff) - 1970; $mo = gmdate('n', $diff) - 1; $d = gmdate('j', $diff) - 1; $h = gmdate('G', $diff); $mi = ltrim(gmdate('i', $diff), '0'); $s = ltrim(gmdate('s', $diff), '0'); return array( ($y == 1) ? 'year' : 'years' => $y, ($mo == 1) ? 'month' : 'months' => $mo, ($d == 1) ? 'day' : 'days' => $d, ($h == 1) ? 'hour' : 'hours' => $h, ($mi == 1) ? 'minute' : 'minutes' => $mi, ($s == 1) ? 'second' : 'seconds' => $s ); } else { die('endTime parameter must be larger than startTime parameter in timeDifference function.'); } } // Sample usage $array = timeDifference('2008-08-20 12:00:00'); foreach($array as $unit => $value) { echo $value, ' ', $unit, '<br />'; } ?> The ProjectFear one work perfectly, I won't try another one, Thank anyway Link to comment https://forums.phpfreaks.com/topic/122222-time-elapsed/#findComment-631082 Share on other sites More sharing options...
thebadbad Posted September 1, 2008 Share Posted September 1, 2008 Yeah, when you only need hours, minutes and seconds (didn't realize until now) use his code. But if you wanna calculate time differences larger than one month, my function would be more precise than one done similar to ProjectFear's code, because seconds per year and month aren't constant. Link to comment https://forums.phpfreaks.com/topic/122222-time-elapsed/#findComment-631084 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.