My PHP script for generating calendar bugs in october - php

I made a simple PHP script for generating calendar. It has rounded weeks (I mean if month starts on Friday it will generate it monday of that week), navigation, etc. Works great, but there is a bug in October. It draws last sunday of month twice. I use sundays as a singnal for new row so it makes
Example of my calendar
Example of calendar with October bug
And here the code (I'm not professional, I learned PHP on my own, with no books, just with google and PHP manual):
<?php
function getfirstday($month, $year)
{
$datestr = "01-$month-$year";
$day = date('N', strtotime($datestr));
return $day;
}
function getlastday($month, $year)
{
$datestr = cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year";
$day = date('N', strtotime($datestr));
return $day;
}
//Don't care about this, I just want to have weekdays in my primary language
function getweekday($weekDay) {
$list = array();
$list['1'] = "Pondělí";
$list['2'] = "Úterý";
$list['3'] = "Středa";
$list['4'] = "Čtvrtek";
$list['5'] = "Pátek";
$list['6'] = "Sobota";
$list['7'] = "Neděle";
return $list[$weekDay];
}
//What month and year do we want to show?
//Ger it from URL or use current
$month = $_GET['month'];
if ($month == "") {
$month = date('m');
}
$year = $_GET['year'];
if ($year == "") {
$year = date('Y');
}
//Firts day of month
$startDayStr = "01-$month-$year";
//Some calculation to get interval of whole month and rounded weeks
$startDay = strtotime($startDayStr) - (getfirstday($month, $year ) - 1) * 24*60*60;
$roundMonth = 7 - getlastday($month, $year );
$limit = cal_days_in_month(CAL_GREGORIAN, $month, $year ) + (getfirstday($month, $year ) - 1) + $roundMonth;
//Some navigation
?>
<table>
<tr>
<?php
if ($month > 1) {
$prevm = $month - 1;
$prevh = "cal.php?month=$prevm&year=$year";
} elseif ($month == 1) {
$prevm = 12;
$prevy = $year -1;
$prevh = "cal.php?month=$prevm&year=$prevy";
}
if ($month < 12) {
$nextm = $month +1;
$nexth = "cal.php?month=$nextm&year=$year";
} elseif ($month == 12) {
$nextm = 1;
$nexty = $year +1;
$nexth = "cal.php?month=$nextm&year=$nexty";
}
?>
<td width="200" align="left"><Previous month</td>
<td width="190" align="center">
<?php echo date('m', strtotime("01-$month-$year 00:00:00")); ?>
</td>
<td width="200" align="right">Next month></td>
</tr>
</table>
<?php
//Let's generate our calendar
echo "<table border=\"1\"><tr>";
for($j=1;$j<=7;$j++){
echo "<td align=\"center\" width=\"85\" height=\"50\"><b>".getweekday($j)."</b></td>";
}
echo "</tr><tr>";
for($i = 1;$i <= $limit;$i++) {
$lastDayOfMonth = strtotime(cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year 23:59:59");
$firstDayOfMonth = strtotime("01-$month-$year");
$weekDay = date('N', $startDay);
if ($startDay < $firstDayOfMonth || $startDay > $lastDayOfMonth) {
$class = "caltdb";
} else {
$class = "caltda";
}
echo "<td class=\"$class\" align=\"center\" height=\"40\">". date('d', $startDay) ."</td>";
if ($weekDay == '7') {
echo "</tr><tr>";
}
$startDay = $startDay + 24*60*60;
}
echo "</tr></table>";
?>
Could you help me fix this problem? I dont know why is it happening.
Thank you a lot,
Heretiiik

The problem is likely due to the fact that you're using + 24*60*60 to add one day to a timestamp. This causes problems with daylight saving time, because there are days with 23 or 25 hours when DST begins/ends.
Near the end of your script, replace:
$startDay = $startDay + 24*60*60;
with:
$startDay = strtotime('+1 day', $startDay);

Related

php calendar with previous and next not reloading the page

I need to include a php calendar on the sidebar of my page.
I am using a snippet I found weeks ago, as I have used it before and it works fine. But this time, I have to add next and previous buttons to display the previous or next month...
My question is... do I need to modify the php that generates the current month?
this is what I have so far:
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
$today = date("d"); // Current day
$month = date("m"); // Current month
$year = date("Y"); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><span class="dayWrap">'. $date . '</span></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
</table>
<div class="changeMonthLinks">
<a class="col-xs-12" href="">< Prev</a>
<a class="col-xs-12 aright" href="">Next ></a>
</div>
I just don't know how to proceed...or what do I need to add in the anchor tags :S
Any help will be appreciate it.
Thank you
Thank you!!
I've added $now for the param ?now in the url and I'm parsing it with strtotime to the variable $dtNow, all the date functions are extended and the links in the bottom are extended with ?now=$dtNow + 1 month and ?now=$dtNow - month
Here's the code
<?php
$now = '';
if(isset($_GET['now']))
$now = $_GET['now'];
$dtNow = strtotime($now);
if(!$dtNow)
{
$dtNow = time();
}
echo "<h1>Today is " . date('Y-m-d', $dtNow) . "</h1>";
?>
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
$today = date("d", $dtNow); // Current day
$month = date("m", $dtNow); // Current month
$year = date("Y", $dtNow); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><span class="dayWrap">'. $date . '</span></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
</table>
<div class="changeMonthLinks">
<a class="col-xs-12" href="?now=<?php echo date('Y-m-d', $dtNow - 30*24*60*60); ?>">< Prev</a>
<a class="col-xs-12 aright" href="?now=<?php echo date('Y-m-d', $dtNow + 30*24*60*60); ?>">Next ></a>
</div>

php calendar previous button continuously incrementing

"Thank you I have already solved my problem I think this one work"
I have a code that display only mondays of the month.
The next button is not calculating well.
Supposed to be it well end on 12(Month).
But it continuously adding the month.
This is my PHP code.
<?php
$current_month = date("n");
$month = ($_GET['m']) ? $_GET['m'] : date("n");
$previous_month = ($month - 1);
$next_month = ($month + 1);
$year = ($_GET['y']) ? $_GET['y'] : date("Y");
$previous_year = $year;
if($month == 0)
{
$month = 12;
$year--;
}
if($month == 13)
{
$month = 1;
$year++;
}
if($previous_month == 0)
{
$previous_month = 12;
$previous_year--;
}
$startDate = $year."-".$month."-01";
$endDate = $year."-".$month."-31";
$endDate = strtotime($endDate);
echo("<form name = 'formCalendar' id = 'formCalendar' action = 'calendar1.php?' method = 'get'>");
echo '<table border=1>';
echo '<tr>';
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
echo '<td>'.date('d-M-y', $i).'</td>';
echo '</tr>';
echo(" </select>");
echo(" <input type = 'button' name = 'prev' value = '<<' onclick = 'location=\"calendar1.php?m={$previous_month}&y={$previous_year}\"'/>");
echo(" <input type = 'button' name = 'next' value = '>>' onclick = 'location=\"calendar1.php?m=". ($month + 1)."&y={$year}\"'/>");
echo(" </table>");
echo("<form>");
?>
Pls help fix the problem.
here is your complete working code
<?php
$current_month = date("n");
$month = (isset($_GET['m'])) ? $_GET['m'] : date("n");
$year = (isset($_GET['y'])) ? $_GET['y'] : date("Y");
$previous_month = ($month - 1);
$next_month = ($month + 1);
$previous_year = $year;
$next_year = $year;
if($previous_month==0)
{
$previous_month = 12;
$previous_year = $year-1;
}
if($next_month>12)
{
$next_month = 1;
$next_year = $year+1;
}
$startDate = $year."-".$month."-01";
$endDate = $year."-".$month."-31";
$endDate = strtotime($endDate);
echo("<form name = 'formCalendar' id = 'formCalendar' action = 'calender1.php?' method = 'get'>");
echo '<table border=1>';
echo '<tr>';
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
echo '<td>'.date('d-M-y', $i).'</td>';
echo '</tr>';
echo(" </select>");
echo(" <input type = 'button' name = 'prev' value = '<<' onclick = 'location=\"calender1.php?m={$previous_month}&y={$previous_year}\"'/>");
echo(" <input type = 'button' name = 'next' value = '>>' onclick = 'location=\"calender1.php?m={$next_month}&y={$next_year}\"'/>");
echo(" </table>");
echo("<form>");
?>
Try this:
if ($month == 12) {
$previous_month = $month;
$next_month = 1;
} else {
$previous_month = ($month - 1);
$next_month = ($month + 1);
}
That solves the month issue
This question maybe little old post but I hope it helps to finetune the answer especially on the month issue when cross over the year
$previous_month = date("m", strtotime("-1 month", $year."-".$month."-01"));
$next_month = date("m", strtotime("+1 month", $year."-".$month."-01"));

How to start a calendar from monday with PHP

I have an event calendar that starts on Sunday. I must change it to start on Monday.
Part of my code:
<html>
<body>
<?php
$dagteller=$firstDayArray["wday"];
$mDay=$firstDayArray["mday"];
define("ADAY", (60*60*24));
$mydate=getdate(date("U"));
define("ADAY", (60*60*24));
for ($count=0; $count < (6*7); $count++) {
$dayArray = getdate($start);
if (($count % 7) == 0) {
if ($dayArray["mon"] != $month) {
break;
} else {
echo ("</tr ><tr>\n");
}
}
if ((!isset($_POST['month'])) || (!isset($_POST['year']))) {
$nowArray = getdate();
$month = $nowArray['mon'];
$year = $nowArray['year'];
$day = $nowArray['day'];
} else {
$month = $_POST['month'];
$year = $_POST['year'];
}
// on my table
echo ("<td bgcolor=\"#DDDDDD\"><center>".$dayArray["mday"]."</center></td>\n");
$start += ADAY;
?>
</body>
</html>
Read the PHP date manual for yourself:
http://www.php.net/manual/en/function.date.php
The 'W' identifier will help you

Creation of weekly calender in php

I have created a php calender which will show one week at a time.Here is the code i have created
<?php
$week = date("W");
$year = (isset($_GET['year']))?$_GET['year']:date("Y");
$week = (isset($_GET['week']))?$_GET['week']:Date('W');
if($week>53){
$year+= 1;
$week=1;
}
?>
Next Week <!--Next week-->
Pre Week <!--Previous week-->
<table border="1px">
<tr>
<td>Employee</td>
<?php
for($day=1; $day<=7; $day++)
{
$d = strtotime($year."W".$week.$day);
echo "<td>".date('l',$d )."<br>";
echo date('d M',$d)."</td>";
}
?>
</tr>
when i am trying to go to the next week it is working correctly. But when the year is changing it is not working for the next year.
Leave the week calculation to the DateTime::setIsoDate() method.
Here is the simplest and best solution for your problem :
<?php
$dt = new DateTime;
if (isset($_GET['year']) && isset($_GET['week'])) {
$dt->setISODate($_GET['year'], $_GET['week']);
} else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
}
$year = $dt->format('o');
$week = $dt->format('W');
?>
Pre Week <!--Previous week-->
Next Week <!--Next week-->
<table>
<tr>
<td>Employee</td>
<?php
do {
echo "<td>" . $dt->format('l') . "<br>" . $dt->format('d M Y') . "</td>\n";
$dt->modify('+1 day');
} while ($week == $dt->format('W'));
?>
</tr>
</table>
When you're using strtotime, your week has to be two-digit. You have to prepend a zero, if the week is lower than 10 before the for loop.
if($week < 10) {
$week = '0'. $week;
}
for($day = 1; $day <= 7; $day++) {
Also, a year only has 52 weeks, the condition at the beginning should be.
if($week > 52) {
$year++;
$week = 1;
} elseif($week < 1) { // If you want the possibility to go back too
$year--;
$week = 52;
}
Full code:
<?php
$year = (isset($_GET['year'])) ? $_GET['year'] : date("Y");
$week = (isset($_GET['week'])) ? $_GET['week'] : date('W');
if($week > 52) {
$year++;
$week = 1;
} elseif($week < 1) {
$year--;
$week = 52;
}
?>
Next Week <!--Next week-->
Pre Week <!--Previous week-->
<table border="1px">
<tr>
<td>Employee</td>
<?php
if($week < 10) {
$week = '0'. $week;
}
for($day= 1; $day <= 7; $day++) {
$d = strtotime($year ."W". $week . $day);
echo "<td>". date('l', $d) ."<br>". date('d M', $d) ."</td>";
}
?>
</tr>
</table>

PHP Beginner...Simple PHP syntax error

I'm just getting started on my PHP journey and was completing a tutorial for the creation of a simple calendar. I'm running into a syntax error in Codepad and haven't been able to find a fix. I'm sure it's something simple that I'm not seeing. Sorry about the notes, I've been trying to annotate as much as possible so I don't get lost.
The error is:
Parse error: syntax error, unexpected ',' on line 10. (the $day=('d', $date) declaration)
Code:
<?php
// current date variable
$date = time ();
//day, month and year variables
$day = ('d', $date);
$month = ('m', $date);
$year = ('Y', $date);
// first day of the month
$monthfirstday = mktime(0,0,0,$month, 1, $year);
// get the name of the month
$monthtitle = ('F', $monthfirstday);
// first day of the week
$weekday = ('D', $monthfirstday);
// identify the days of the week
switch ($weekday) {
case"Sun": $blank=0;
break;
case"Mon": $blank=1;
break;
case"Tue": $blank=2;
break;
case"Wed": $blank=3;
break;
case"Thu": $blank=4;
break;
case"Fri": $blank=5;
break;
case"Sat": $blank=6;
break;
}
// number of days in the month
$daysinmonth = cal_days_in_month(0, $month, $year);
// include the html
echo "<div id='calendar-wrap'>";
echo "<table border=6 width=394><tr><th colspan=60> $monthtitle $year</th></tr>";
echo "
<tr>
\n\t\t<td width=62>SUN</td>
\n\t\t<td width=62>MON</td>
\n\t\t<td width=62>TUES</td>
\n\t\t<td width=62>WEDS</td>
\n\t\t<td width=62>THURS</td>
\n\t\t<td width=62>FRI</td>
\n\t\t<td width=62>SAT</td>
</tr>
";
$daycount = 1;
echo "<tr>";
// dealing with the days of the month
$blank > 0
{
echo "<td></td>";
$blank = $blank-1;
$daycount++;
}
// set the day number to 1
$daynumber = 1;
// count the days of the month
while
( $daynumber <= $daysinmonth )
{
echo "<td> $daynumber </td>";
// increase the day count until the month ends
$daynumber++;
$daycount++;
// add a new row every 7 days
if ($daycount > 7)
{
echo "</tr><tr>";
$daycount = 1;
}
}
// fill in blank days if necessary
while
($daycount > 1 && $daycount <= 7)
{
echo "<td> </td>";
$daycount++;
}
echo "</tr></table></div>";
?>
Thanks in advance,
Mike
Your function is missing!
$day = date('d', $date);
Here you go...
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
I think you're trying to do this:
$day = date('d', $date);
This is the Error. Missed the Function :)
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

Categories