PHP – Create an Array of Hours

If you need to create an array of store hours (for example), the following function can generate an associative array of values quickly, with the hours formatted and stepped (every hour, 30 mins, etc.) as you wish.

I’ve seen similar functions using DateTime(), but we’re already working with timestamps, so there’s really no need.

Example usage:

Here’s the returned array, using the all the default argument values (24 hours, stepped every hour, formatted as h:mm am/pm):