Announcement

Collapse
No announcement yet.

Adding 24hr time to get decimal result

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding 24hr time to get decimal result

    Ok 1 more question for you bofins today

    A form I have has the following fields

    Start Time {st} "time"
    Finish Time {ft} "time"
    Overtime {ot} "int"
    Overtime x2 {ot2} "int"
    Total {t} "int"

    The start and finish times use the timepicker function which is saved as 23:30 (11:30pm) so 24hr or mitilary time whatever you want to call it and the other fields are just standard 2 decimal place numbers eg: ot=2.25, I may look at making this a dropdown or similar formatted field???

    What I need to work out is how to convert this military time to a decimal and add the final 2 fields together before giving a final result all on the fly if possible..... I currently have a onchange {t}={ot2}+{ot} working but the time conversion part is confusing.

    I'm not that experianced in any form of programming so a laymans explanation would be appreciated.

  • #2
    First of all, your fields {t}, {ot} and {ot2} have to be of type decimal with precision 2, NOT integer.
    There is not much of a conversion, just a division of the minutes by 60 to get the decimal value of an hour.
    My suggestion is using the php date_diff() function (please read!).

    http://php.net/manual/en/function.date-diff.php

    Here we go:
    Code:
    $start = date_create({st}); //create a php datetime object
    $finish = date_create({ft});
    $t_diff = date_diff($start, $finish); //calculate the difference
    {t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up
    jsb
    Last edited by jsbinca; 06-06-2015, 11:23 AM.

    Comment


    • #3
      thats awesome mate thanks, exactly what I was after...... the only issue I have is adding up past midnight, for example if a employee starts at 23:00 11pm and finished at 03:00 = 4 hrs yet the code returns 20, would this need a if statement for this condition?

      Comment


      • #4
        Format your start and finish fields as datetime not just time.
        Or you can do something like:
        Code:
        if($finish < $start)
        {
        	$finish->add(new DateInterval('P1D'));
        }
        But this only works on time differences < 24:00 hours
        jsb
        Last edited by jsbinca; 06-06-2015, 12:28 PM.

        Comment


        • #5
          Originally posted by jsbinca View Post
          Format your start and finish fields as datetime not just time.
          Or you can do something like:
          Code:
          if($finish < $start)
          {
          	$finish->add(new DateInterval('P1D'));
          }
          But this only works on time differences < 24:00 hours
          jsb
          Thank again mate but neither work,

          1. setting as datetime is ok for the db but not to reflect it on the form via onchange, besides I like the timepicker!
          2.
          if($finish < $start)
          {
          $finish->add(new DateInterval('P1D'));
          }
          dosent work as P1D = 24hrs so we're back at the original problem, I did find "PT12H" but that didn't work either.

          I hate this time thing
          Last edited by w33tbix; 06-06-2015, 01:21 PM.

          Comment


          • #6
            Originally posted by w33tbix View Post
            I hate this time thing
            Why? It's nothing different than any other stuff. Just keep a cool head.
            This does work.
            Code:
            $start = date_create({t1});
            $finish = date_create({t2});
            if($finish < $start)
            {
            	$finish->add(new DateInterval('P1D'));
            }
            $t_diff = date_diff($start, $finish); //calculate the difference
            {t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up
            jsb

            Comment


            • #7
              Originally posted by jsbinca View Post
              Why? It's nothing different than any other stuff. Just keep a cool head.
              This does work.
              Code:
              $start = date_create({t1});
              $finish = date_create({t2});
              if($finish < $start)
              {
              	$finish->add(new DateInterval('P1D'));
              }
              $t_diff = date_diff($start, $finish); //calculate the difference
              {t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up
              jsb
              Nope it's not working as I dont get a result at all, try adding up 23:00 - 01:00, works without the if statement apart from not adding up past 24hrs

              Comment


              • #8
                Er.....Um...... Sorry

                It does work, I forgot to modify the vars to suit my app on the if statement.

                Thanks again JSB, lets hope I can figure the rest of the application out from now on

                Comment


                • #9
                  NVM I need to open my eyes..... 12hrs of debugging
                  Last edited by w33tbix; 06-10-2015, 02:23 PM.

                  Comment


                  • #10
                    A little more help on this subject would be wonderfull, I'm not a programmer but understand a little.

                    This

                    Code:
                    $start = date_create({t1});
                    $finish = date_create({t2});
                    if($finish < $start)
                    {
                    	$finish->add(new DateInterval('P1D'));
                    }
                    $t_diff = date_diff($start, $finish); //calculate the difference
                    {t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up
                    Works perfectly but I've been asked if we can get it to round to the nearest 1/4hr interval in 100min clock format eg; 01:00, 01:25, 01:50, 01:75 etc, currently 09:05 and 15:25 = 6.33 where I'd like it to add up to 6.25

                    I've read the PHP Round function http://php.net/manual/en/function.round.php but just cant figure how it would best fit in the above.

                    Thanks again

                    Comment


                    • #11
                      Round({t}* 4) / 4

                      jsb

                      Comment


                      • #12
                        Thanks again JSB but I cant figure out wherte the above snippet fits within the code, I've tried several places I would expect it to go yet the result remains 6.33

                        Comment


                        • #13
                          It would be easier if I could modify the timepicker to only allow the selection of 15min increments 00, 15, 30, 45 on the minute side

                          Comment


                          • #14
                            Still need help with this..... I cant for the life of me work out where that code goes

                            Comment


                            • #15
                              Right there
                              Code:
                              $start = date_create({t1});
                              $finish = date_create({t2});
                              if($finish < $start)
                              {
                              	$finish->add(new DateInterval('P1D'));
                              }
                              $t_diff = date_diff($start, $finish);
                              {t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2};
                              {t} = round({t}*4)/4;
                              // or as a one-liner {t} = round(($t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2})*4)/4;
                              jsb

                              Comment

                              Working...
                              X