Calculating Number of days in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • realin
    Contributor
    • Feb 2007
    • 254

    Calculating Number of days in php

    hi guys..

    I have made a function which counts the numbers of days or hours or minutes from the current datetime to the give datetime.. but i am confused while displaying number of days along with number of hours.. rest all works fine(I Suppose).
    here is the code :
    [CODE=PHP]

    function day_counter($da te,$msg){
    $today=time();

    $day_to_count=s trtotime($date) ;
    if($day_to_coun t>$today){
    $rem=$day_to_co unt-$today;
    }

    else {if($today>$day _to_count){
    $rem=$today-$day_to_count;

    }}

    if($rem>=86400) {
    $days=$rem/86400;

    $days=round($da ys,2);
    $dys=(int)$days ;
    $h=(float)strst r($days,".");

    $h=round($h*24) ;
    if($h==24)
    $dys+=1;
    if($h<=0 OR $h==24)
    return "<b>".$dys. "</b> Days ".$msg;
    return "<b>".$dys. "</b> Days <b>".$h."</b> Hours ".$msg;
    }
    if($rem>=3600){
    $hours=$rem/3600;
    $hours=round($h ours,2);
    $hrs=(int)$hour s;
    $mn=(float)strs tr($hours,".");
    $mn=ceil($mn*60 );
    if($mn<=0)
    return "<b>".$hrs. "</b> Hours ".$msg;
    return "<b>".$hrs. "</b> Hours <b>".$mn."</b> Minutes ";
    }

    if($rem>=60){
    $min=$rem/60;
    $min=round($min ,2);
    $mnm=(int)$min;
    $sec=(float)str str($min,".");
    $sec=ceil($sec* 60);
    if($sec<=0)
    return "<b>".$mnm. "</b> Minutes ".$msg;
    return "<b>".$mnm. "</b> Minutes <b>".$sec." </b>Seconds";
    }
    if($rem>0 && $rem<60){
    return "<b>".$rem. "</b> Seconds".$msg;
    }
    }[/CODE]

    And i want to know, is this the right way to get the decimal part of a number ?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, realin.

    [code=php]
    $fpart = $val - (int) $val;
    [/code]

    Comment

    • realin
      Contributor
      • Feb 2007
      • 254

      #3
      Originally posted by pbmods
      Heya, realin.

      [code=php]
      $fpart = $val - (int) $val;
      [/code]
      hi again !!

      thanks for the reply.. but is the rest of code,allright ?? i mean it doesnt calculate the number of hours accurately in the first case.. that means along with the number of days..

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Realin.

        [code=php]
        $diff = abs(time() - strtotime($date ));

        $days = (int) ($diff / 86400);
        $day_remainder = $diff % 86400;

        $hours = (int) ($day_remainder / 3600);
        $hour_remainder = $day_remainder % 3600;
        [/code]

        And so on....

        I did it the long way the first time, too ~_^

        Comment

        • realin
          Contributor
          • Feb 2007
          • 254

          #5
          Originally posted by pbmods
          Heya, Realin.

          [code=php]
          $diff = abs(time() - strtotime($date ));

          $days = (int) ($diff / 86400);
          $day_remainder = $diff % 86400;

          $hours = (int) ($day_remainder / 3600);
          $hour_remainder = $day_remainder % 3600;
          [/code]

          And so on....

          I did it the long way the first time, too ~_^

          woah thanks.. pretty similar results.. but the code is short.. :)
          thanks a lot

          Comment

          Working...