datediff not accurate.

Things you’d like to miss in the future...
Forum rules
When reporting a bug, please include the following information: your XYplorer version (e.g., v27.90.0047), your Windows version (e.g., Win 11), and your screen scaling percentage (e.g., 125%). We recommend adding your Windows version and screen scaling percentage to your profile or signature. This will make debugging much easier for us.
zer0
Posts: 2676
Joined: 19 Jan 2009 20:11

Re: datediff not accurate.

Post by zer0 »

Can you please use code tags for your code? Thank you :roll:
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

admin
Site Admin
Posts: 64838
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: datediff not accurate.

Post by admin »

I'll add ceil and floor in the next version.

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: datediff not accurate.

Post 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
Power-hungry user!!!

admin
Site Admin
Posts: 64838
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: datediff not accurate.

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

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: datediff not accurate.

Post 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! ;)
Power-hungry user!!!

j_c_hallgren
XY Blog Master
Posts: 5826
Joined: 02 Jan 2006 19:34
Location: So. Chatham MA/Clearwater FL
Contact:

Re: datediff not accurate.

Post 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.
Still spending WAY TOO much time here! But it's such a pleasure helping XY be a treasure!
(XP on laptop with touchpad and thus NO mouse!) Using latest beta vers when possible.

admin
Site Admin
Posts: 64838
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: datediff not accurate.

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

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: datediff not accurate.

Post 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.
Power-hungry user!!!

Post Reply