Calculating Future Dates with PHP

I recently received a request to set up a form that displays a recurring schedule of dates and times, allowing visitors to request one of those blocks of time as an appointment. The schedule would be the same every week, with numerous available blocks of time on specific days of each week.

I had two choices; I could either manually figure out each of the dates and then insert them into the database (either by-hand or with a script of some sort), which would require me to update the database to add new future dates rather frequently, or I could figure out a way to let PHP figure out the dates for me. I chose the latter, and I’ll show you how I did it after the jump.

Some Handy YouTube Tricks

I’m sure there have been times when you’ve wanted to link to a particular part of a YouTube video but, if you’re anything like me, you had no idea how to go about doing so. Well, you have two options:

  1. You can link to the video on YouTube and specify the minute and second at which you want the video to start playing.
  2. You can embed the video and specify the second at which you would like the video to start playing.

PHP Date and Time Functions

PHP offers a few really nice functions to manipulate dates and times. With the functions built into PHP, you can easily pull individual parts of a date or time and use it or format it just about any way you want.

strtotime()

To start, you should really familiarize yourself with PHP’s strtotime() function. The strtotime() function takes a standard date (and optional time) and converts it into a Unix timestamp. Because the strtotime() function returns boolean false if it doesn’t recognize the input as a valid date/time, you can also use it to check the validity of a date.