While digging through a bunch of data I collected, I needed a function to pull a week’s worth of hour ranges stored via a Unix timestamp.  For example, I wanted all data between midnight and 4am on every day for the last week.  I didn’t find anything while googling for a copy/paste solution, so I thought I would share my solution.

/* unixBetweenHours
 * Convert time between a string range, to a unix timestamp range 
 *
 * @param 	string 	$start 	Beginning time range (ex: '3am' or '03:00')
 * @param 	string 	$stop 	End of time range (ex: '5am' or '05:00')
 * @returns 	array 	All times within the previous week where keys are start times, 
 *                      and values end times
 *
 * Example return: array(7) { [1362045600]=> int(1362052800) ...
 */
function unixBetweenHours($start,$stop) {
	$start = strtotime($start);
	$stop = strtotime($stop);
	$realtime = time();

	// Prevent future times from being returned
	if ($start > $realtime || $stop > $realtime) {
		$start = $start - 86400;
		$stop = $stop - 86400;
	}

	$ret = null;
	for($x = 0;$x < 7; $x++) {
		$s = $start - ($x * 86400); // -1 day
		$e = $stop - ($x * 86400);
		$ret[$s] = $e;
	}
	return $ret;
}

Here are two example outputs.

var_dump(unixBetweenHours('00:00','03:00'));
/*
Returns:
array(7) { 
    [1362114000]=> int(1362124800) 
    [1362027600]=> int(1362038400) 
    [1361941200]=> int(1361952000) 
    [1361854800]=> int(1361865600) 
    [1361768400]=> int(1361779200) 
    [1361682000]=> int(1361692800) 
    [1361595600]=> int(1361606400) 
} 
*/
foreach(unixBetweenHours('00:00','03:00') AS $s => $e) {
	echo date("F j, Y, g:i a",$s) . " THRU " . date("F j, Y, g:i a",$e) . "
";
}
/*  For human readability:
Returns:
March 1, 2013, 12:00 am THRU March 1, 2013, 3:00 am
February 28, 2013, 12:00 am THRU February 28, 2013, 3:00 am
February 27, 2013, 12:00 am THRU February 27, 2013, 3:00 am
February 26, 2013, 12:00 am THRU February 26, 2013, 3:00 am
February 25, 2013, 12:00 am THRU February 25, 2013, 3:00 am
February 24, 2013, 12:00 am THRU February 24, 2013, 3:00 am
February 23, 2013, 12:00 am THRU February 23, 2013, 3:00 am
*/

If the time given is in the future, it returns the previous 7 days starting from yesterday, rather than today.