Page 2 of 2

Re: datediff not accurate.

Posted: 21 Mar 2011 02:12
by zer0
Can you please use code tags for your code? Thank you :roll:

Re: datediff not accurate.

Posted: 21 Mar 2011 09:16
by admin
I'll add ceil and floor in the next version.

Re: datediff not accurate.

Posted: 26 Mar 2011 17:15
by tiago
Despite admin's helpful additions to math, I am still looking after a reliable method to convert seconds in hours and minutes. Took 4 different approaches and still stuck on this. Any help...?

14398 secs = 3h 59min 58sec

Re: datediff not accurate.

Posted: 26 Mar 2011 19:30
by admin
tiago wrote:Despite admin's helpful additions to math, I am still looking after a reliable method to convert seconds in hours and minutes. Took 4 different approaches and still stuck on this. Any help...?

14398 secs = 3h 59min 58sec
Try this for starters (not throroughly tested). Indeed integer division and modulo are applied:

Code: Select all

// format duration given in seconds
  $DAYS_PER_YEAR = 365.25;
  $HOURS_PER_DAY = 24;
  $MINS_PER_HOUR = 60;
  $SECS_PER_MIN = 60;
  $duration = "";
  
  $Secs = 14398;
  
  $Mins = $Secs \ $SECS_PER_MIN;
  $Hours = $Mins \ $MINS_PER_HOUR;
  $Days = $Hours \ $HOURS_PER_DAY;
  $Years = $Days \ $DAYS_PER_YEAR;
  If ($Years > 0) {
    $duration = $Years . "y";
  }
  $Days = $Days % $DAYS_PER_YEAR;
  If ($Days > 0) {
    $duration = $duration . " " . $Days . "d";
  }
  $Hours = $Hours % $HOURS_PER_DAY;
  If ($Hours > 0) {
    $duration = $duration . " " . $Hours . "h";
  }
  $Mins = $Mins % $MINS_PER_HOUR;
  If ($Mins > 0) {
    $duration = $duration . " " . $Mins . "min";
  }
  $Secs = $Secs % $SECS_PER_MIN;
  If ($Secs > 0) {
    $duration = $duration . " " . $Secs . "sec";
  }
  echo $duration;

Re: datediff not accurate.

Posted: 26 Mar 2011 20:48
by tiago
It worked after a little upgrade, have to learn to keep an eye on your beta releases. Works like a charm, admin! Sorry for bothering even after all the implements you made, missed that "integer division" upgrade. THAT'S precision enough! ;)

Re: datediff not accurate.

Posted: 26 Mar 2011 23:24
by j_c_hallgren
tiago wrote:It worked after a little upgrade, have to learn to keep an eye on your beta releases. Works like a charm, admin!
Yes, we know that one must check at least daily for newest vers here! :lol: And sometimes more!

And yes, Don is 'admin' here but prefers his name to that title, I think.

Re: datediff not accurate.

Posted: 27 Mar 2011 10:30
by admin
Here's an improved version (in the last version the return had a leading space character):

Code: Select all

// format duration given in seconds
  $DAYS_PER_YEAR = 365.25;
  $HOURS_PER_DAY = 24;
  $MINS_PER_HOUR = 60;
  $SECS_PER_MIN = 60;
  $sep = " ";
  $duration = "";
  
  $Secs = 14398;
  
  $Mins = $Secs \ $SECS_PER_MIN;
  $Hours = $Mins \ $MINS_PER_HOUR;
  $Days = $Hours \ $HOURS_PER_DAY;
  $Years = $Days \ $DAYS_PER_YEAR;
  If ($Years > 0) {
    $duration = $duration . $sep . $Years . "y";
  }
  $Days = $Days % $DAYS_PER_YEAR;
  If ($Days > 0) {
    $duration = $duration . $sep . $Days . "d";
  }
  $Hours = $Hours % $HOURS_PER_DAY;
  If ($Hours > 0) {
    $duration = $duration . $sep . $Hours . "h";
  }
  $Mins = $Mins % $MINS_PER_HOUR;
  If ($Mins > 0) {
    $duration = $duration . $sep . $Mins . "min";
  }
  $Secs = $Secs % $SECS_PER_MIN;
  If ($Secs > 0) {
    $duration = $duration . $sep . $Secs . "sec";
  }

  // cut initial sep
  $duration = substr($duration, strlen($sep));

  echo $duration;

Re: datediff not accurate.

Posted: 28 Mar 2011 15:06
by tiago
Thank you, I easily integrated your script into those mine which were lacking such as my only functional code was ruined when floating appear.