leequalls Posted March 9, 2010 Share Posted March 9, 2010 I am hoping this is a simple to fix problem. I have a schedule where viewers can change the day they are looking at at the top of the page it displays the date for that day. So say you were looking at the schedule on Tuesday, March 09, 2010 then click on Friday at the top of Fridays page it would say Friday, March 12, 2010 I have this part working fine. The problem is at the end of the month if you were to do this it would say Friday, March 33, 2010 instead of Friday, April 02, 2010 if($_GET['week'] == ""){ $day = date("l", (mktime() - ($serverOffset * 3600) + ($offset * 3600))); $_GET['week'] = date("l", (mktime() - ($serverOffset * 3600) + ($offset * 3600))); } if ($_GET['offset'] == "" && $_GET['offset'] != 0) { $offset = $serverOffset; $_GET['offset'] = $serverOffset; } // Find timestamp for day if not current day $aDayValues = array( 'MONDAY' => 1, 'TUESDAY' => 2, 'WEDNESDAY' => 3, 'THURSDAY' => 4, 'FRIDAY' => 5, 'SATURDAY' => 6, 'SUNDAY' => 7 ); // Number of days in month $aDayMonth = array( 'January' => 31, 'February' => 28, 'March' => 31, 'April' => 30, 'May' => 31, 'June' => 30, 'July' => 31, 'August' => 31, 'September' => 30, 'October' => 31, 'November' => 30, 'December' => 31 ); if (date('L') == "1") { $aDayMonth[February] = "29"; } //Leap Year $scheduleTime = mktime() - ($serverOffset * 3600) + ($offset * 3600); if (strtoupper($day) != strtoupper(date("l", (mktime() - ($serverOffset * 3600) + ($offset * 3600))))) { $specDay = strtoupper($day); $onDay = strtoupper(date("l", mktime() - ($serverOffset * 3600) + ($offset * 3600))); if ($aDayValues[$specDay] > $aDayValues[$onDay]) { $dayInc = 1; } else { $dayInc = -1; } $testTime = mktime() - ($serverOffset * 3600) + ($offset * 3600); while (strtoupper(date("l", $testTime)) != strtoupper($day)) { $testTime += (60 * 60 * 24) * $dayInc; } $scheduleTime = $testTime; } // Prepare days to grab $aGrabDays = array( $day ); // Add additional day if necessary if ($offset < $serverOffset) { array_push($aGrabDays, date("l", $scheduleTime + (60 * 60 * 24))); } else if ($offset > $serverOffset) { array_push($aGrabDays, date("l", $scheduleTime - (60 * 60 * 24))); } // Start SQL Query $sql = <<<SQL SELECT * FROM rp_timetable WHERE `day` IN ( SQL; foreach ($aGrabDays as $onDay) { $sql .= <<<SQL '$onDay', SQL; } $sql = preg_replace('#,$#iu', "", $sql); $sql .= <<<SQL ) SQL; $offsetDifference = $offset - $serverOffset; // Populate hours to grab $aGrabHours = array(); $aAdjustedHours = array(); for ($i = 0; $i < 24; $i++) { if (($i - $offsetDifference) < 0) { $hour = ($i - $offsetDifference + 24) + 1; // Add one to stay in line with the table $onDay = date("l", $scheduleTime - (60 * 60 * 24)); // Previous day $tday = date('N')-1; } else if (($i - $offsetDifference) > 23) { $hour = ($i - $offsetDifference - 24) + 1; // Add one to stay in line with the table $onDay = date("l", $scheduleTime + (60 * 60 * 24)); // Next Day $tday = date('N')+1; } else if ($offsetDifference == 0) { $hour = $i + 1; $onDay = date("l", $scheduleTime); // Today $tday = date('N'); } else { $hour = $i - $offsetDifference + 1; $onDay = date("l", $scheduleTime); // Today $tday = date('N'); } if (isset($onDay) && isset($hour)) { $onDay = strtoupper($onDay); if (!is_array($aGrabHours[$onDay])) { $aGrabHours[$onDay] = array(); } if (!is_array($aAdjustedHours[$onDay])) { $aAdjustedHours[$onDay] = array(); } array_push($aGrabHours[$onDay], $hour); $aAdjustedHours[$onDay][$hour] = $i + 1; $mmon = date('F'); $dday = date('d'); $yyea = date('Y'); $addmath = $aDayValues[$onDay] - $tday; if($dday == $aDayMonth[$mmon]) { $addmath += 0; } else { $addmath += $dday; } $submath = $tday - $aDayValues[$onDay]; $submath = $dday - $submath; if ($aDayValues[$onDay] != $tday) { if ($aDayValues[$onDay] > $tday) { $minfo = $addmath; if($minfo <= "9") {$minfo = "0".$minfo;} } else { $minfo = $submath; if($minfo <= "9") {$minfo = "0".$minfo;} }} else { $minfo = date('d'); } } unset($onDay); unset($hour); } } print <<<HTML <font style='font-size: 18px;'>-You are currently viewing $day, $mmon $minfo, $yyea in $aOffsetTimezone[$offset]- </font> <p> HTML; Link to comment https://forums.phpfreaks.com/topic/194595-date-script/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.