03c06-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 57 行
PHP
57 行
<?php// Set the default timezone to US/Eastern time:date_default_timezone_set('US/Eastern');// This function will count the number of business days between two datesfunction count_business_days($a, $b) { // First, sort these. We need to know which one comes first if ($a < $b) { $first = $a; $second = $b; } else { $first = $b; $second = $a; } // Break these timestamps up into their constituent parts: $f = getdate($first); $s = getdate($second); // Calculate the number of business days in the first week left. // Do this by subtracting the number of the day of the week from Friday $f_days = 5 - $f['wday']; // If it was Saturday or Sunday you will get a -1 or 5 but we want 0 if (($f_days == 5) || ($f_days < 0)) { $f_days = 0; } // Do the same for the second week except count to the beginning of // the week. However, make sure that Saturday only counts as 5 $s_days = ($s['wday'] > 5) ? 5 : $s['wday']; // Calculate the timestamp of midday, the Sunday after the first date: $f_sunday = mktime(12, 0, 0, $f['mon'], $f['mday'] + ((7 - $f['wday']) % 7), $f['year']); // And the timestamp of midday, the Sunday before the second date: $s_sunday = mktime(12, 0, 0, $s['mon'], $s['mday'] - $s['wday'], $s['year']); // Calculate the full weeks between these two dates by substracting // them, then dividing by the seconds in a week. You need to round // this afterwards to always ensure an even number. Otherwise // daylight savings time can offset the calculation. $weeks = round(($s_sunday - $f_sunday) / (3600*24*7)); // Return the number of days by multiplying weeks by 5 and adding // the extra days: return ($weeks * 5) + $f_days + $s_days;}// Try a couple of examples:$date1 = strtotime('12/3/1973 8:13am');$date2 = strtotime('1/15/1974 10:15pm');$date3 = strtotime('2/14/2005 1:32pm');// Calculate the business days between, They are: 31 & 8109echo "<p>There are ", count_business_days($date1, $date2), " days.</p>";echo "<p>There are ", count_business_days($date2, $date3), " days.</p>";?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?