Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This example uses PHP's built in DateTime classes to do the date math. How I approached this was to start by counting the number of full working days between the two dates and then multiply that by 8 (see notes). Then it gets the hours worked on the partial days and adds them to the total hours worked. Turning this into a function would be fairly straightforward to do.</p> <p><strong>Notes:</strong></p> <ul> <li>Does not take timestamps into account. But you already know how to do that.</li> <li>Does not handle holidays. (That can be easily added by using an array of holidays and adding it to where you filter out Saturdays and Sundays).</li> <li>Requires PHP 5.3.6+</li> <li>Assumes an 8 hour workday. If employees do not take lunch change <code>$hours = $days * 8;</code> to <code>$hours = $days * 8.5;</code></li> </ul> <p>.</p> <pre><code>&lt;?php // Initial datetimes $date1 = new DateTime('2012-03-22 11:29:16'); $date2 = new DateTime('2012-03-24 03:58:58'); // Set first datetime to midnight of next day $start = clone $date1; $start-&gt;modify('+1 day'); $start-&gt;modify('midnight'); // Set second datetime to midnight of that day $end = clone $date2; $end-&gt;modify('midnight'); // Count the number of full days between both dates $days = 0; // Loop through each day between two dates $interval = new DateInterval('P1D'); $period = new DatePeriod($start, $interval, $end); foreach ($period as $dt) { // If it is a weekend don't count it if (!in_array($dt-&gt;format('l'), array('Saturday', 'Sunday'))) { $days++; } } // Assume 8 hour workdays $hours = $days * 8; // Get the number of hours worked on the first day $date1-&gt;modify('5:30 PM'); $diff = $date1-&gt;diff($start); $hours += $diff-&gt;h; // Get the number of hours worked the second day $date1-&gt;modify('8 AM'); $diff = $date2-&gt;diff($end); $hours += $diff-&gt;h; echo $hours; </code></pre> <p><strong><a href="http://3v4l.org/sOtfX" rel="nofollow">See it in action</a></strong></p> <p><strong>Reference</strong></p> <ul> <li><a href="http://www.php.net/manual/en/class.datetime.php" rel="nofollow">DateTime Class</a></li> <li><a href="http://www.php.net/manual/en/class.dateperiod.php" rel="nofollow">DatePeriod Class</a></li> <li><a href="http://www.php.net/manual/en/class.dateinterval.php" rel="nofollow">DateInterval Class</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload