| PHP Developer Forum Hier habt ihr die Möglichkeit, eure Skriptprobleme mit anderen Anwendern zu diskutieren. Seid so fair und beantwortet auch Fragen von anderen Anwendern. Dieses Forum ist sowohl für ANFÄNGER als auch für PHP-Profis! Post your PHP questions here! |
 |

16-08-2009, 00:55
|
|
Jester_Prince
Registrierter Benutzer
|
|
Registriert seit: Jul 2009
Ort: Berlin
Beiträge: 33
|
|
Kalender-Script funktioniert bei Schaltjahren nicht
Guten Abend/Morgen!
Heute habe ich ein Problem, bei dem ich nicht ganz weiß, ob ich einfach nur schusselig bin oder ob es tatsächlich an einer Lücke in meinem Wissen über die Datumsfunktionen von PHP geht.
Ich habe mir ein Script für eine Kalenderfunktion geschrieben. Diese ist vielleicht nicht perfekt, aber sie ist vollständig auf meinem Mist gewachsen und deshalb bin ich an sich sehr zufrieden damit, zumal das Script, bis auf eine Sache super funktioniert.
Bei einem Schaltjahr funktioniert das Script nämlich nicht und ich kann einfach nicht den Grund dafür herausfinden.
Es wäre klasse, wenn mir jemand mit meinem Problem helfen könnte.
Das Script sieht wie folgt aus:
PHP-Code:
<?php
// The current month $month = date("n"); // The current year $year = date("Y"); // The current day $day = date("j");
if(isset($_SESSION['posted'])) { $date = $_SESSION['posted']; $set = "yes"; $year = substr($date, 0, 4); $month = substr($date, 5, 2); $day = substr($date, 8, 2); }
function displayCalendar($month, $year, $day, $date, $set) {
$cal = "<table cellspacing=\"0\" id=\"calendar\" summary=\"Calendar table\">\n"; $cal .= "\t<caption>" . strftime("%B %Y", strtotime($date)) . "</caption>\n"; $cal .= "\t<thead>\n"; $cal .= "\t\t<tr>\n"; $cal .= "\t\t\t<th id=\"calendarHead\" scope=\"col\" title=\"Monday\">Mon</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Tuesday\">Tue</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Wednesday\">Wed</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Thursday\">Thu</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Friday\">Fri</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Saturday\">Sat</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Sunday\">Sun</th>\n"; $cal .= "\t\t</tr>\n"; $cal .= "\t</thead>\n"; $cal .= "\t<tbody>\n";
// The first day of the current month $first_day_in_month = date("D", mktime(0, 0, 0, $month, 1, $year));
// How many days does the current month have? $month_has_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
// An array containing the days of the week in sequence $day_nr = array("Mon" => 1, "Tue" => 2, "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6, "Sun" => 7);
// The number of the first table data element containing a date $begin_at_day_nr = $day_nr[$first_day_in_month];
// The total number of tr tags $trs = ceil(($begin_at_day_nr + $month_has_days)/7);
// The total number of td tags $total_tds = $trs * 7;
for($i=1; $i<=$total_tds; $i++) {
// Begin with a tr tag if($i == 1) { $cal .= "\t\t<tr class=\"odd\">\n"; }
// Seven table data tags per row are needed, so put // a </tr> after every seventh td tag if($i % 7 == 1 AND $i != 1) { if($i-$month_has_days != 0) { $cal .= "\t\t</tr>\n"; // Assign a class to every second tr tag if($i % 2 == 1) { $cal .= "\t\t<tr class=\"odd\">\n"; } else { $cal .= "\t\t<tr>\n"; } } }
// Since not every month starts with the same week day there will // have to be some td tags without a calendar day. Fill them with // the word "empty" and use CSS with a high negative text-indent // value to make them appear empty, in order to keep the Markup valid if($i<$begin_at_day_nr OR $i-$begin_at_day_nr+1>$month_has_days) { $cal .= "\t\t\t<td class=\"empty\">empty</td>\n"; } else { // Substract the number of empty tds from the actual number of tds // in order to display the correct calendar day $cal_day = $i-$begin_at_day_nr+1; if($cal_day < 10) { $cal_day = "0". (string)$cal_day; } if($cal_day == $day) { if($set == "yes") { $cal .= "\t\t\t<td class=\"marked\" title=\"Current blog\">" . $cal_day . "</td>\n"; } else { $cal .= "\t\t\t<td class=\"marked\" title=\"Today's date\">" . $cal_day . "</td>\n"; } } else { $cal .= "\t\t\t<td>" . $cal_day . "</td>\n"; } } }
$earlier = date("F", mktime(0, 0, 0, $month-1, 0, $year)); $later = date("F", mktime(0, 0, 0, $month+2, 0, $year));
$cal .= "\t\t</tr>\n"; $cal .= "\t</tbody>\n"; $cal .= "</table>\n";
return $cal;
}
echo displayCalendar($month, $year, $day, $date, $set);
unset($date, $_SESSION['posted'], $set);
?>
Beachtet einfach den obersten Block nicht, und gebt einmal die Daten eines Schaltjahres ein, bspw.:
PHP-Code:
echo displayCalendar(02, 2004, $day, $date, $set);
Dann werdet Ihr sehen, dass die letzte Reihe von table data tags nicht mehr von table row tags umschlossen wird. Und es scheint tatsächlich nur bei Schaltjahren nicht zu funktionieren.
Ich komme einfach nicht auf den Fehler und bin für jede Hilfe dankbar!
EDIT: Ich hoffe, der Code ist nicht zu unübersichtlich.
Geändert von Jester_Prince (16-08-2009 um 02:59 Uhr)
|

16-08-2009, 02:51
|
|
Jester_Prince
Registrierter Benutzer
|
|
Registriert seit: Jul 2009
Ort: Berlin
Beiträge: 33
|
|
Ich habe den Fehler selbst gefunden.
PHP-Code:
if($i % 7 == 1 AND $i != 1) { if($i-$month_has_days != 0) { /* In dieser überflüssigen Abfrage liegt der Fehler */ $cal .= "\t\t</tr>\n"; if($i % 2 == 1) { $cal .= "\t\t<tr class=\"odd\">\n"; } else { $cal .= "\t\t<tr>\n"; } } }
Ich muss vergessen haben, diese Zeile nach einem Test aus dem Script zu löschen. Wenn man zu lange an etwas arbeitet, geht die Konzentration eben verloren.
Geändert von Jester_Prince (16-08-2009 um 02:54 Uhr)
|

16-08-2009, 03:15
|
|
Jester_Prince
Registrierter Benutzer
|
|
Registriert seit: Jul 2009
Ort: Berlin
Beiträge: 33
|
|
PHP-Code:
<?php
$date = date("Y-m-H"); $set = "no"; $month = date("n"); $year = date("Y"); $day = date("j");
function displayCalendar($month, $year, $day, $date, $set) {
$cal = "<table cellspacing=\"0\" id=\"calendar\" summary=\"Calendar table\">\n"; $cal .= "\t<caption>" . strftime("%B %Y", strtotime($date)) . "</caption>\n"; $cal .= "\t<thead>\n"; $cal .= "\t\t<tr>\n"; $cal .= "\t\t\t<th id=\"calendarHead\" scope=\"col\" title=\"Monday\">Mon</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Tuesday\">Tue</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Wednesday\">Wed</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Thursday\">Thu</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Friday\">Fri</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Saturday\">Sat</th>\n"; $cal .= "\t\t\t<th scope=\"col\" title=\"Sunday\">Sun</th>\n"; $cal .= "\t\t</tr>\n"; $cal .= "\t</thead>\n"; $cal .= "\t<tbody>\n";
// The first day of the current month $first_day_in_month = date("D", mktime(0, 0, 0, $month, 1, $year));
// How many days does the current month have? $month_has_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
// An array containing the days of the week in sequence $day_nr = array("Mon" => 1, "Tue" => 2, "Wed" => 3, "Thu" => 4, "Fri" => 5, "Sat" => 6, "Sun" => 7);
// The number of the first table data element containing a date $begin_at_day_nr = $day_nr[$first_day_in_month];
// The total number of tr tags $trs = ceil(($begin_at_day_nr + $month_has_days-1)/7);
// The total number of td tags $total_tds = $trs * 7;
for($i=1; $i<=$total_tds; $i++) {
// Begin with a tr tag if($i == 1) { $cal .= "\t\t<tr class=\"odd\">\n"; }
// Seven table data tags per row are needed, so put // a </tr> after every seventh td tag if($i % 7 == 1 AND $i != 1) { if($i-$first_day_in_month-7 != 0) { $cal .= "\t\t</tr>\n"; // Assign a class to every second tr tag if($i % 2 == 1) { $cal .= "\t\t<tr class=\"odd\">\n"; } else { $cal .= "\t\t<tr>\n"; } } }
// Since not every month starts with the same week day there will // have to be some td tags without a calendar day. Fill them with // the word "empty" and use CSS with a high negative text-indent // value to make them appear empty, in order to keep the Markup valid if($i<$begin_at_day_nr OR $i-$begin_at_day_nr+1>$month_has_days) { $cal .= "\t\t\t<td class=\"empty\">empty</td>\n"; } else { // Substract the number of empty tds from the actual number of tds // in order to display the correct calendar day $cal_day = $i-$begin_at_day_nr+1; if($cal_day < 10) { $cal_day = "0". (string)$cal_day; } if($cal_day == $day) { if($set == "yes") { $cal .= "\t\t\t<td class=\"marked\" title=\"Current blog\">" . $cal_day . "</td>\n"; } else { $cal .= "\t\t\t<td class=\"marked\" title=\"Today's date\">" . $cal_day . "</td>\n"; } } else { $cal .= "\t\t\t<td>" . $cal_day . "</td>\n"; } } }
$earlier = date("F", mktime(0, 0, 0, $month-1, 0, $year)); $later = date("F", mktime(0, 0, 0, $month+2, 0, $year));
$cal .= "\t\t</tr>\n"; /* $cal .= "\t\t<tr>\n"; $cal .= "\t\t\t<td colspan=\"3\" class=\"monthLink\"><< " . $earlier . "</td>\n"; $cal .= "\t\t\t<td class=\"empty noHover\">empty</td>\n"; $cal .= "\t\t\t<td colspan=\"3\" class=\"monthLink\">" . $later . " >></td>\n"; $cal .= "\t\t</tr>\n"; */ $cal .= "\t</tbody>\n"; $cal .= "</table>\n";
return $cal;
}
echo displayCalendar($month, $year, $day, $date, $set);
unset($date, $_SESSION['posted'], $set);
So! Nun arbeitet das Script genau so, wie es soll. Es geht vielleicht einfacher, effektiver und vielleicht auch schöner, aber es geht doch nichts über eigenen Code.
Wer die Funktion benutzen/erweitern/verbessern möchte, kann dies gerne tun. Es wäre aber schön, wenn Ihr Eure Version dann auch hier postet, damit jeder was davon hat.
Und hier noch einmal ein Bild von der CSS formatierten Version meines Kalenders ;-)
|

16-08-2009, 13:32
|
 |
Berni
  OWNER
|
|
Registriert seit: Jan 2001
Ort: Frankfurt / Egelsbach
Beiträge: 6.206
|
|
danke für deine die Lösung!
Viele werden vor ähnlichen Problemen stehen
|
|
Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
|
|
|
| Themen-Optionen |
|
|
| Thema bewerten |
|
|
Forumregeln
|
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
HTML-Code ist aus.
|
|
|
|
PHP News
|