galdikas Posted May 17, 2011 Share Posted May 17, 2011 So i coded investment bank, and now I need to come up with a way to return remaining time. In the database I record a timestamp. Lets say for a week long investment I will say: $timespan=time()+604800; and then store it in database. So for converting it to some thing like: xx days xx hours xx minutes xx seconds of investment time remaining. I found this function: function date_diff($start, $end="NOW") { $sdate = strtotime($start); $edate = strtotime($end); $time = $edate - $sdate; if($time>=0 && $time<=59) { // Seconds $timeshift = $time.' seconds '; } elseif($time>=60 && $time<=3599) { // Minutes + Seconds $pmin = ($edate - $sdate) / 60; $premin = explode('.', $pmin); $presec = $pmin-$premin[0]; $sec = $presec*60; $timeshift = $premin[0].' min '.round($sec,0).' sec '; } elseif($time>=3600 && $time<=86399) { // Hours + Minutes $phour = ($edate - $sdate) / 3600; $prehour = explode('.',$phour); $premin = $phour-$prehour[0]; $min = explode('.',$premin*60); $presec = '0.'.$min[1]; $sec = $presec*60; $timeshift = $prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '; } elseif($time>=86400) { // Days + Hours + Minutes $pday = ($edate - $sdate) / 86400; $preday = explode('.',$pday); $phour = $pday-$preday[0]; $prehour = explode('.',$phour*24); $premin = ($phour*24)-$prehour[0]; $min = explode('.',$premin*60); $presec = '0.'.$min[1]; $sec = $presec*60; $timeshift = $preday[0].' days '.$prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '; } return $timeshift; } Since i use it with time stamps, not with dates, i changed it a little: //$time_from_db is $timespan=time()+604800; function date_diff(time(), $time_from_db) { //remove the strtotime(), since I use timestamps already. $sdate =$start; $edate =$end; $time = $edate - $sdate; if($time>=0 && $time<=59) { // Seconds $timeshift = $time.' seconds '; } elseif($time>=60 && $time<=3599) { // Minutes + Seconds $pmin = ($edate - $sdate) / 60; $premin = explode('.', $pmin); $presec = $pmin-$premin[0]; $sec = $presec*60; $timeshift = $premin[0].' min '.round($sec,0).' sec '; } elseif($time>=3600 && $time<=86399) { // Hours + Minutes $phour = ($edate - $sdate) / 3600; $prehour = explode('.',$phour); $premin = $phour-$prehour[0]; $min = explode('.',$premin*60); $presec = '0.'.$min[1]; $sec = $presec*60; $timeshift = $prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '; } elseif($time>=86400) { // Days + Hours + Minutes $pday = ($edate - $sdate) / 86400; $preday = explode('.',$pday); $phour = $pday-$preday[0]; $prehour = explode('.',$phour*24); $premin = ($phour*24)-$prehour[0]; $min = explode('.',$premin*60); $presec = '0.'.$min[1]; $sec = $presec*60; $timeshift = $preday[0].' days '.$prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '; } return $timeshift; } Quote Link to comment Share on other sites More sharing options...
galdikas Posted May 17, 2011 Author Share Posted May 17, 2011 Ok.. figured it out myself. Here is end function: function date_diff($stamp) { $time = $stamp; if($time>=0 && $time<=59) { // Seconds $timeshift = $time.' seconds '; } elseif($time>=60 && $time<=3599) { // Minutes + Seconds $pmin = $time / 60; $premin = explode('.', $pmin); $presec = $pmin-$premin[0]; $sec = $presec*60; $timeshift = $premin[0].' min '.round($sec,0).' sec '; } elseif($time>=3600 && $time<=86399) { // Hours + Minutes $phour = $time / 3600; $prehour = explode('.',$phour); $premin = $phour-$prehour[0]; $min = explode('.',$premin*60); $presec = '0.'.$min[1]; $sec = $presec*60; $timeshift = $prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '; } elseif($time>=86400) { // Days + Hours + Minutes $pday = $time / 86400; $preday = explode('.',$pday); $phour = $pday-$preday[0]; $prehour = explode('.',$phour*24); $premin = ($phour*24)-$prehour[0]; $min = explode('.',$premin*60); $presec = '0.'.$min[1]; $sec = $presec*60; $timeshift = $preday[0].' days '.$prehour[0].' hrs '.$min[0].' min '.round($sec,0).' sec '; } return $timeshift; } Dont have time to explain more at the moment. But if anyone need more explanation, just post here, and will explain more how to use it :) Quote Link to comment Share on other sites More sharing options...
Dayo Posted May 17, 2011 Share Posted May 17, 2011 im guessing you are trying to find out how many days, hours, minutes and seconds there are left check out my timestamp tutorial: mainly $ts->tstotime($var); all you would have to do is add the class then add $time=$ts->tstotime(($timespan-time())); echo $time['d'].'Days '.$time['h'].'Hours '.$time['m'].'Minutes '.$time['s'].'seconds' Quote Link to comment Share on other sites More sharing options...
galdikas Posted May 22, 2011 Author Share Posted May 22, 2011 I am not yet object oriented :P But this could be used with procedural as well?? Thanks for tip ;) Quote Link to comment 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.