Calculate Working Hours Between Two Dates PHP

Discussion in 'PHP' started by FishSword, Oct 21, 2012.

  1. #1
    A typical working day for me starts at 09:00 to 15:00.


    If a project was to be signed off before or after these hours, how do I calulate the number of hours I have had the signed project for during work hours, based on the sign off date and todays date?

    Example:



    • Date/Time Now: 20/10/2012 10:00
    • Date/Time Project Signed: 20/10/2012 07:00

    Then the number of working hours I have had the project signed for is 1 hour.
     
    FishSword, Oct 21, 2012 IP
  2. navigator10

    navigator10 Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can make the diference between the days and convert the result in hours and the diference betwheen hours and after this calculate the amount of this 2 results
     
    navigator10, Oct 21, 2012 IP
  3. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I sort of know what I need to do, I just have issues coding it!
     
    FishSword, Oct 21, 2012 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Do you have any code yet?

    Here is what I would as per logic:

    
    // go through each our
    number of hours = 0
    for x = strtotime(start time); x <= strototime(time now); x += 3600 {
        // check if this hour is within 9-15
        hour = date("H", x)
        if (hour >= 9 && hour <= 15) {
            number of hours ++;
        }
    }
    
    PHP:
     
    ThePHPMaster, Oct 21, 2012 IP