RobertP Posted February 22, 2012 Share Posted February 22, 2012 i am having an issue with a function, seems it dose work, but at the same time it does not. sad i need to turn a number, (seconds count) into a string. here is what i have function timeToString($seconds){ $seconds = ceil($seconds); $string = array(); $days = floor($seconds/60/60/24); $hours = floor(($seconds-($days*60*60*24))/60/60); $mins = floor(($seconds-($hours*60*60))/60); $secs = floor(($seconds-($mins*60+$hours*60*60))); if($days>0) $string[] = $days.' Day(s)'; if($hours>0) $string[] = $hours.' Hour(s)'; if($mins>0) $string[] = $mins.' Minute(s)'; if($secs>0) $string[] = $secs.' Second(s)'; return (count($string)>0) ? trim(implode(', ',$string)) : 'n/a'; } example: timeToString(12)=12 Second(s) timeToString(60)=1 Minute(s) timeToString(121)=2 Minute(s), 1 Second(s) timeToString(333)=5 Minute(s), 33 Second(s) timeToString(9999)=2 Hour(s), 46 Minute(s), 39 Second(s) timeToString(9392042)=108 Day(s), 16 Hour(s), 155574 Minute(s), 2 Second(s) Quote Link to comment https://forums.phpfreaks.com/topic/257497-function-timetostring/ Share on other sites More sharing options...
scootstah Posted February 22, 2012 Share Posted February 22, 2012 And the issue is ... ? Quote Link to comment https://forums.phpfreaks.com/topic/257497-function-timetostring/#findComment-1319810 Share on other sites More sharing options...
RobertP Posted February 22, 2012 Author Share Posted February 22, 2012 large numbers results in timeToString(9392042)=108 Day(s), 16 Hour(s), 155574 Minute(s), 2 Second(s) Quote Link to comment https://forums.phpfreaks.com/topic/257497-function-timetostring/#findComment-1319813 Share on other sites More sharing options...
Philip Posted February 22, 2012 Share Posted February 22, 2012 <?php function timeToString($seconds) { if(!is_int($seconds) || $seconds < 0) $seconds = 0; $seconds = ceil($seconds); $time = array(); $string = array(); $secondsTranslations = array('day' => 60 * 60 * 24, 'hour' => 60 * 60, 'min' => 60, 'sec' => 1); foreach($secondsTranslations as $key => $trans) { $time[$key] = floor($seconds / $trans); $seconds = $seconds - ($time[$key] * $trans); } if($time['day']>0) $string[] = $time['day'].' Day(s)'; if($time['hour']>0) $string[] = $time['hour'].' Hour(s)'; if($time['min']>0) $string[] = $time['min'].' Minute(s)'; if($time['sec']>0) $string[] = $time['sec'].' Second(s)'; return (count($string)>0) ? trim(implode(', ',$string)) : 'n/a'; } $tests = array(-15, 12, 60, 121, 333, 9999, 9392042); foreach($tests as $test) { echo $test . ': ' . timeToString($test) . '<br />'; } Returns -15: n/a 12: 12 Second(s) 60: 1 Minute(s) 121: 2 Minute(s), 1 Second(s) 333: 5 Minute(s), 33 Second(s) 9999: 2 Hour(s), 46 Minute(s), 39 Second(s) 9392042: 108 Day(s), 16 Hour(s), 54 Minute(s), 2 Second(s) Quote Link to comment https://forums.phpfreaks.com/topic/257497-function-timetostring/#findComment-1319820 Share on other sites More sharing options...
RobertP Posted February 22, 2012 Author Share Posted February 22, 2012 i have this working thank you everyone for your help. function secondsToTime($seconds){ $hours = floor($seconds/(60*60)); $days = floor($hours/24); if($days>0) $hours = floor($hours-($days*24)); $weeks = floor($days/7); if($weeks>0) $days = floor($days-($weeks*7)); $months = floor($weeks/4); if($months>0) $weeks = floor($weeks-($months*4)); $years = floor($months/12); if($years>0) $months = floor($months-($years*12)); $minuteDivisor = $seconds%(60*60); $minutes = floor($minuteDivisor/60); $secondDivisor = $minuteDivisor%60; $seconds = ceil($secondDivisor); return array( 'y'=>(int)$years, 'n'=>(int)$months, 'w'=>(int)$weeks, 'd'=>(int)$days, 'h'=>(int)$hours, 'm'=>(int)$minutes, 's'=>(int)$seconds, ); } function timeToString($seconds){ $time = secondsToTime($seconds); if($time['y']>0) $string[] = $time['y'].' Years(s)'; if($time['n']>0) $string[] = $time['n'].' Months(s)'; if($time['w']>0) $string[] = $time['w'].' Weeks(s)'; if($time['d']>0) $string[] = $time['d'].' Day(s)'; if($time['h']>0) $string[] = $time['h'].' Hour(s)'; if($time['m']>0) $string[] = $time['m'].' Minute(s)'; if($time['s']>0) $string[] = $time['s'].' Second(s)'; return (count($string)>0) ? trim(implode(', ',$string)) : 'n/a'; } Quote Link to comment https://forums.phpfreaks.com/topic/257497-function-timetostring/#findComment-1319836 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.