Jump to content

Calendar, week starts with Sunday


Worqy

Recommended Posts

Hello.

 

I have made a very simple calendar with the help of a tutorial. http://www.codewalkers.com/c/a/Date-Time-Code/Simple-PHP-Calendar/

 

Now the problem is, that the week start with Sunday, insted of Monday.

 

The table is looks like this (See attached image)

 

The code is:

<?php 
date_default_timezone_set('Europe/Helsinki');
//This gets today's date
$date =time () ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ; 

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){ 
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; 
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 

//Here we start building the table heads 
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td 
width=42>T</td><td width=42>W</td><td width=42>T</td><td 
width=42>F</td><td width=42>S</td></tr>";

//This counts the days in the week, up to 7
$day_count = 1;

echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 ) 
{ 
echo "<td></td>"; 
$blank = $blank-1; 
$day_count++;
} 

//sets the first day of the month to 1 
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month ) 
{ 
echo "<td> $day_num </td>"; 
$day_num++; 
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 

//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 ) 
{ 
echo "<td> </td>"; 
$day_count++; 
} 
echo "</tr></table>"; 
?>

 

So what I want is that the week starts from Monday, not Sunday.

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Educated guess...

 

re-arrange the switch statment numbering - make mon=0, tues=1, wed=2 etc etc and finally sun=6

 

Okey, now there is just one little problem. I made some changes in the code, so you can change month and year. But now the month allways starts from sunday, monday is allways the second, 8, 15 date...

 

In other words, the dates are allways the same. But, the count days in month system still works.

 

The new code is...

<?php 
date_default_timezone_set('Europe/Helsinki');

// Get...
$month = $_POST['ViewMonth'];
if($month == ""){
$month = "01";
}
$year= $_POST['ViewYear'];
if($year == ""){
$year = "2010";
}
// Controls
?>
<form action="test.php" method="POST">
<select name="ViewMonth">
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
<option name="4">4</option>
<option name="5">5</option>
<option name="6">6</option>
<option name="7">7</option>
<option name="8">8</option>
<option name="9">9</option>
<option name="10">10</option>
<option name="11">11</option>
<option name="12">12</option>
</select>
<select name="ViewYear">
<option name="2006">2006</option>
<option name="2007">2007</option>
<option name="2008">2008</option>
<option name="2009">2009</option>
<option name="2010">2010</option>
<option name="2011">2011</option>
<option name="2012">2012</option>
<option name="2013">2013</option>
<option name="2014">2014</option>
</select>
<input type="submit" value="Go">
</form>
<?php

//This gets today's date
$date =time () ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
//$month = date('m', $date) ;
//$year = date('Y', $date) ;

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ; 

//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){ 
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; 
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 

//Here we start building the table heads 
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td 
width=42>T</td><td width=42>W</td><td width=42>T</td><td 
width=42>F</td><td width=42>S</td></tr>";

//This counts the days in the week, up to 7
$day_count = 1;

echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 ) 
{ 
echo "<td></td>"; 
$blank = $blank-1; 
$day_count++;
} 

//sets the first day of the month to 1 
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month ) 
{ 
echo "<td> $day_num </td>"; 
$day_num++; 
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 

//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 ) 
{ 
echo "<td> </td>"; 
$day_count++; 
} 
echo "</tr></table>"; 
?>

 

Hope someone can help..

Link to comment
Share on other sites

Here you go.

 

I added some code to it, both to correct it, and for functionality. All new code has comments, and is marked by //******

<?php 
date_default_timezone_set('Europe/Helsinki');

// Get...
$month = $_POST['ViewMonth'];
if($month == ""){
$month = "01";
}
$year= $_POST['ViewYear'];
if($year == ""){
$year = "2010";
}
// Controls
?>
<form action="start.monday.php" method="POST">
<select name="ViewMonth">
<?php for($i = 1; $i < 13; $i++) { //******added for loop, so you could keep the last selected month.
echo '<option ';
echo ($i == (int)$month) ? 'selected="selected"' : NULL;
echo '>' . $i . '</option>';
}
?>
</select>
<select name="ViewYear">
<?php for($i = 2006; $i < 2015; $i++) { //******added teh for loop, so you could keep the last selected year.
echo '<option ';
echo ($i == (int)$year) ? 'selected="selected"' : NULL;
echo '>' . $i . '</option>';
}
?>
</select>
<input type="submit" value="Go">
</form>
<?php

//This gets today's date
$date =time () ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
//$month = date('m', $date) ;
//$year = date('Y', $date) ;

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;
//*****This line had been deleted, making the switch Useless.
//Here we find out what day of the week the first day of the month falls on $day_of_week = date('D', $first_day) ; 
$day_of_week = date('D',$first_day);
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){  //******days re-ordered.
case "Sun": $blank =6; break; 
case "Mon": $blank = 0; break; 
case "Tue": $blank = 1; break; 
case "Wed": $blank = 2; break; 
case "Thu": $blank = 3; break; 
case "Fri": $blank = 4; break; 
case "Sat": $blank = 5; break; 
}

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 

//Here we start building the table heads 
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
//*******Changed echo string to reflect that 1st day is Monday.
echo "<tr><td width=42>M</td><td width=42>T</td><td 
width=42>W</td><td width=42>T</td><td width=42>F</td><td 
width=42>S</td><td width=42>S</td></tr>";

//This counts the days in the week, up to 7
$day_count = 1;

echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 ) 
{ 
echo "<td></td>"; 
$blank = $blank-1; 
$day_count++;
} 

//sets the first day of the month to 1 
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month ) 
{ 
echo "<td> $day_num </td>"; 
$day_num++; 
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 

//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 ) 
{ 
echo "<td> </td>"; 
$day_count++; 
} 
echo "</tr></table>"; 
?>

 

Problems.

 

As suggested re-ordered switch.

$day_of_week variable was missing, put it in.

re-ordered column names to report Monday as first day of the week.

 

Functionality.

 

Replaced static dropdowns with for loops, so you could keep the last selected month and year.

Link to comment
Share on other sites

Okey thank you, works great!

Now to the last thing, set in events.

I need my calendar to see if there is any events on this days...

I have a little idea, that everytime you change month, the script would connect to mysql and see if there is a match with dates. But how to do this, I need some help.

 

The code so far: http://pastebin.com/iC5wUt5H

 

Worqy

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.