Note that there are some explanatory texts on larger screens.

plurals
  1. POcalc the working hours between 2 dates in PHP
    primarykey
    data
    text
    <p>I have a function to return the difference between 2 dates, however I need to work out the difference in working hours, <strong>assuming Monday to Friday (9am to 5:30pm):</strong></p> <pre><code>//DATE DIFF FUNCTION // Set timezone date_default_timezone_set("GMT"); // Time format is UNIX timestamp or // PHP strtotime compatible strings function dateDiff($time1, $time2, $precision = 6) { // If not numeric then convert texts to unix timestamps if (!is_int($time1)) { $time1 = strtotime($time1); } if (!is_int($time2)) { $time2 = strtotime($time2); } // If time1 is bigger than time2 // Then swap time1 and time2 if ($time1 &gt; $time2) { $ttime = $time1; $time1 = $time2; $time2 = $ttime; } // Set up intervals and diffs arrays $intervals = array('year','month','day','hour','minute','second'); $diffs = array(); // Loop thru all intervals foreach ($intervals as $interval) { // Set default diff to 0 $diffs[$interval] = 0; // Create temp time from time1 and interval $ttime = strtotime("+1 " . $interval, $time1); // Loop until temp time is smaller than time2 while ($time2 &gt;= $ttime) { $time1 = $ttime; $diffs[$interval]++; // Create new temp time from time1 and interval $ttime = strtotime("+1 " . $interval, $time1); } } $count = 0; $times = array(); // Loop thru all diffs foreach ($diffs as $interval =&gt; $value) { // Break if we have needed precission if ($count &gt;= $precision) { break; } // Add value and interval // if value is bigger than 0 if ($value &gt; 0) { // Add s if value is not 1 if ($value != 1) { $interval .= "s"; } // Add value and interval to times array $times[] = $value . " " . $interval; $count++; } } // Return string with times return implode(", ", $times); } </code></pre> <p><strong>Date 1 = 2012-03-24 03:58:58<br> Date 2 = 2012-03-22 11:29:16</strong></p> <p>Is there a simple way of doing this, i.e - calculating the percentage of working hours in a week and dividing the difference using the above function - I have played around with this idea and got some very strange figures...</p> <p>Or is there better way....?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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