Counting Types of Days in PHP

Since I work on our HR team at Automattic, sometimes I need to write code dealing with payroll. When someone starts or leaves we have to pay them for a partial period, which is based on the number of week days. Originally I wrote a simple function, but I set out to make it more useable. Here is what I came up with…

/**
 * Counts the days (optionally limited by type of day) between (inclusive) two dates.
 *
 * @param string $start_date First day (Y-m-d format).
 * @param string $end_date   Last day (Y-m-d format).
 * @param array  $types      Optional. Types of days to count. Default to all types
 *
 * @return int Number of days matching the $types.
 */
function get_days_between_dates( $start_date, $end_date, $types = array() ) {
	$count = 0;

	$included_day_type_indexes = array();
	if ( empty( $types ) ) {
		$included_day_type_indexes = range( 1, 7 );
	} else {
		foreach ( $types as $type ) {
			switch ( strtolower( $type ) ) {
				case 'weekday':
					$included_day_type_indexes = array_merge( $included_day_type_indexes, range( 1, 5 ) );
					break;
				case 'weekend':
					$included_day_type_indexes = array_merge( $included_day_type_indexes, range( 6, 7 ) );
					break;
				case 'monday':
				case 'mon':
					$included_day_type_indexes[] = 1;
					break;
				case 'tuesday':
				case 'tues':
				case 'tue':
				case 'tu':
					$included_day_type_indexes[] = 2;
					break;
				case 'wednesday':
				case 'wed':
					$included_day_type_indexes[] = 3;
					break;
				case 'thursday':
				case 'thurs':
				case 'thur':
				case 'thu':
				case 'th':
					$included_day_type_indexes[] = 4;
					break;
				case 'friday':
				case 'fri':
					$included_day_type_indexes[] = 5;
					break;
				case 'saturday':
				case 'caturday':
				case 'sat':
					$included_day_type_indexes[] = 6;
					break;
				case 'sunday':
				case 'sun':
					$included_day_type_indexes[] = 7;
					break;
			}
		}

		$included_day_type_indexes = array_unique( $included_day_type_indexes );
	}

	$date = strtotime( $start_date );
	$end = strtotime( $end_date );
	while ( $date <= $end ) {
		if ( in_array( date( 'N', $date ), $included_day_type_indexes ) ) {
			$count++;
		}

		$date = strtotime( '+1 day', $date );
	}

	return $count;
}

/*** EXAMPLES ***/

echo get_days_between_dates( '2017-08-21', '2017-09-03' ) . "\n";
echo get_days_between_dates( '2017-08-21', '2017-09-03', array( 'weekday', 'weekend' ) ) . "\n";
echo get_days_between_dates( '2017-08-21', '2017-09-03', array( 'mon' ) ) . "\n";
echo get_days_between_dates( '2017-08-21', '2017-09-03', array( 'weekday' ) ) . "\n";
echo get_days_between_dates( '2017-08-21', '2017-09-03', array( 'mon', 'wednesday', 'fri' ) ) . "\n";

This is the output of the examples…

14
14
2
10
6

What do you think? What would you do different?

I put the function up on GitHub. Submit a pull request if you have improvements.

New Reebok Kicks

When Reebok is giving an additional 30% off in addition to their end of the summer sales, I can’t help myself.

The CrossFit Speed TR has been my favorite shoe for the gym so getting a second pair for $56 was a steal. The color is called “Vitamin C” and looked a lot more orange-ish on the web site when I ordered them. The color of my photo makes them look even different; they are more of a pink/salmon in person. I’ll rock ’em, because I’m not returning them.

For only $28, the Reebok Runner was an easy selection, especially since my size was available in blue. My current running shoes are about a year old, so might be up for retirement. The sockliner in these has a memory foam, which I’m looking forward to trying.

I love buying shoes. 🙂

Link Dump – 2017/08/23

Sound Card Oscilloscope

Earlier this year I came across an old Make post about building your own oscilloscope. I messed around with it a little bit at the time, but I didn’t have the necessary potentiometers, so I set it aside. Then the topic came up again when the tutorials accompanying HackerBox #0018 made use a 3.5mm audio breakout module and some PC oscope software. So in my next Digi-Key order, I got the pots I needed and I picked up some cheap test leads on Amazon. It’s several months later, but I got around to building my own sound card oscilloscope.

First, a couple of notes…

You’ll definitely want to read through the Make post to get familiar with the project. As mentioned in their guide, there really isn’t any good oscope software for the Mac like there is for the PC. With Audacity, which is what I used, at least you can see the signals in wave form.

I spliced an old audio cable. There are several different styles of 3.5mm connectors, but if you’re doing 2 channels, you’ll want to make sure you your cable has the tip, ring, and sleeve.

3.5mm-Male-Connections.png
Source: blog.audio-technica.com

A lot of MacBooks only do mono microphone input. Several Mac bummers in this build! It wasn’t easy, but I found a USB adapter on Amazon that does stereo mic input (most of them only do mono). It’s pricey at almost $30 for something I’m not even sure I’ll use after this build.

Enough of that, on to the build…

I added the LEDs to my build as a visual reference a signal was coming through, but they can be left out, just like in the Make build. If you’re interested in the Fritzing I showed in the video, head over to sound-card-oscilloscope on GitHub. Whenever I’m soldering up a final project I prefer to have the Fritzing for reference instead of looking at my prototype, which typically has a lot of extra wires hanging around. Having a nice clean diagram helps me from making mistakes.

I also found another guide on a site called Home DIY Electronics, which I didn’t end up following.

If you have any questions or build your own version of this, let me know in the comments.

A Full Round With The Reverse Overlap Grip

2017-08-19-scorecard.png

This morning was my first full round with the reverse overlap grip after splitting my previous round between the two styles of overlap. The club is already feeling really good in my hands and a lot more natural than the interlocking grip. I don’t remember the last time I played 18 holes without any penalty strokes.