How to display end of month "rollover"

OK, let me set up my issue.

I have a mySQL database for events. In that database I have a starting date for the event and the duration of the event (default is, of course, 1 day). Now some events are more than 1 day and start on the last day of the month.

So, my problem is how can I display the full duration of the event on my page appropriately? I have to account for the different months having different end dates - along with that pesky leap year.

So, example is that I have an event that starts on March 30, 2012, and is 3 days long. How do I display it as “30-April 1” instead of using the easy way of just adding 3 to the start date (and subtract 1 for good measure) and having the display look like “30-32” which is, of course, incorrect?


Actually you can use it just like that ;), but if you want it formatted as 30 to April 1, you’ll have to use some PHP logic to see if it’s a new month.

$start  = mktime(0,0,0,1,30,2012); # will give you the timestamp for that day.
$end = mktime(0,0,0,1,32,2012); # will give you the time stamp for the 1st of feb. PHP does the rollovers for you automatically 

So with this, you can use date(), to format it as you like.

To check if it’s a new month, you could do something simple like

if (date('m', $start) != date('m', $end)) { # diff month }