From 212205b0ff9ab93cff25431ea60c549f07c2bd74 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Tue, 22 Nov 2011 18:03:47 -0500 Subject: [PATCH] CC-3070: Scheduling show on the 31st of a month causes problem to months that don't have 31 days -fixed. --- airtime_mvc/application/models/Show.php | 37 +++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 2d07001ed..85373d13a 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -1197,12 +1197,39 @@ class Application_Model_Show { self::createRebroadcastInstances($rebroadcasts, $currentUtcTimestamp, $show_id, $show_instance_id, $start, $duration, $timezone); - $dt = new DateTime($start, new DateTimeZone($timezone)); - $dt->add(new DateInterval($p_interval)); - $start = $dt->format("Y-m-d H:i:s"); - $dt->setTimezone(new DateTimeZone('UTC')); - $utcStartDateTime = $dt; + if ($p_interval == 'P1M'){ + /* When adding months, there is a problem if we are on January 31st and add one month with PHP. + * What ends up happening is that since February 31st doesn't exist, the date returned is + * March 3rd. For now let's ignore the day and assume we are always working with the + * first of each month, and use PHP to add 1 month to this (this will take care of rolling + * over the years 2011->2012, etc.). Then let's append the actual day, and use the php + * checkdate() function, to see if it is valid. If not, then we'll just skip this month. */ + + $startDt = new DateTime($start, new DateTimeZone($timezone)); + + /* pass in only the year and month (not the day) */ + $dt = new DateTime($startDt->format("Y-m"), new DateTimeZone($timezone)); + + /* Keep adding 1 month, until we find the next month that contains the day + * we are looking for (31st day for example) */ + do { + $dt->add(new DateInterval($p_interval)); + } while(!checkdate($dt->format("m"), $startDt->format("d"), $dt->format("Y"))); + $dt->setDate($dt->format("Y"), $dt->format("m"), $startDt->format("d")); + + $start = $dt->format("Y-m-d H:i:s"); + + $dt->setTimezone(new DateTimeZone('UTC')); + $utcStartDateTime = $dt; + } else { + $dt = new DateTime($start, new DateTimeZone($timezone)); + $dt->add(new DateInterval($p_interval)); + $start = $dt->format("Y-m-d H:i:s"); + + $dt->setTimezone(new DateTimeZone('UTC')); + $utcStartDateTime = $dt; + } }