Jump to content
MakeWebGames

Recommended Posts

Posted

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;
}

Posted

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 :)

Posted

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'

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...