date format for linux shell scripts

#echo "starting script at" $(date +"%Y-%m-%d %H:%M:%S") "..."

starting publish script at 2012-10-01 15:52:14 …

see: linux-unix-formatting-dates-for-display

PHP date function for calculating workdays in a date range

For a project I needed to get the number of business / working days in a date range. Based on a couple older ones found on PHP.net and SO, I put together an updated PHP function for this purpose. It uses the newer OO date structures for a bit more streamlined code, removed the less efficient strtotime functions and, more importantly, it allows for any days of the week to be considered non-working days.The last argument is non-working days, based on the %w date format where 0=Sunday. You can also pass in an array of holidays which is checked against the non-working days.

view full function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	function getWorkingDays($startDate, $endDate, $holidays=null, $non_wk_day_txt='0,6') {
 
		$non_wk_days = explode(',',$non_wk_day_txt);
 
		$datetime1 = new DateTime($startDate);
		$datetime2 = new DateTime($endDate);
		$interval = $datetime1->diff($datetime2);
 
		$days = (int) $interval->days + 1;
 
		$num_full_weeks = floor($days / 7);
		$num_remaining_days = ($days % 7);
 
		// Sunday=0
		$dow = array(); // days of week
		$dow[0] = $datetime1->format('w');
 
		$rd = $num_remaining_days;
		for ($i=0; $i < $rd; $i++) {
                $d = ($dow[0] + $i) % 7;
			if ( in_array( $d, $non_wk_days) ) {
				$num_remaining_days--; 
			}
		}
 
		$workingDays = $num_full_weeks * (7-count($non_wk_days));
 
		if ($num_remaining_days > 0 )  {
			$workingDays += $num_remaining_days;
		}
 
		// subtract the holidays
		if (!empty($holidays)) {
			foreach ($holidays as $holiday) {
				$h_date = new DateTime($holiday);
 
				if ( !(in_array($h_date->format('w'), $non_wk_days)) ) {
					$workingDays--;
				}
			}
		}
		return $workingDays;
	}