Simple Calendar Functionality & How to skip days into the future
I needed to start dojng something every Monday thru Thursday of the year and I needed the ability to skip Friday, Saturday and Sunday from showing up in my task list. I started thinking about how I would use PHP to tell me what days these were and skip listing Fri, Sat, Sun. I didn't really need to know what had happened in the past, just what would be happening in the future.
If today was a Fri, Sat, or Sun, then I would need to skip showing these days and go straight to the next Monday. I wanted to show a month in an advance at any one time so that I could get a little ahead in my to-do list. These diary entries began with date function codes, so I referenced diary entry: 121799 - Date Codes
I will break up the comment sections of the code and add additional comments.
<?php /*
example38.php -> tracks tasks to be done for specified days of the week
Created: 5/10/03 by TDavid from tdscripts.com
*/
function dayInfo($timestamp) {
global $pretty_date, $day_of_week, $future_count, $i;
$pretty_date = date("D, M j, Y", $timestamp); // Saturday, May 10, 2003
Set for output display of the date in this format
$day_of_week = date("w", $timestamp);
switch(TRUE) {
case $day_of_week == 1 || $day_of_week == 2 || $day_of_week == 3 ||
$day_of_week == 4:
This is an example of how to include multiple TRUE case statements. Let's say you wanted to only show days Monday, Tuesday and Saturday, you would use 1, 2, 6 as the numbers. Days of the week start with 0 (Sunday) and go through 6 (Saturday)
$future_count++;
return true;
break;
}
$i--; // do not advance this iteration
The for loop that moves ahead the future counter for days must be reset and this line does so. If it isn't Friday, Saturday or Sunday, then we just reduce the iteration ($i) by 1. $i-- is the same as writing: $i = $i - 1;
return false;
}
$future_days = 0; // counter of days
$max_days = 24; // show 6 weeks into the future (4 days [m,tu,w,th] X 6 weeks = 24)
$now = time();
$theday = $now; // start with today
for($i=0; $i<$max_days; $i++) {
$D = date("j", $theday);
$M = date("g", $theday);
$YY= date("Y", $theday);
$timestamp = mktime(11,0,0, $M, $D, $Y); // 11 = daylight savings offset, start at noon each day
if(dayInfo($theday)) {
// this is a monday through thursday, so print
print("$pretty_date ($day_of_week):<br>");
}
$theday = $theday + 86400; // add 24 hours to clock
}
print("<br>Total days: <b>$future_count</b>");
?>
Example 38. How to skip dates into the future - source
The above code produces output which looks like this:
Mon, May 12, 2003
(1):
Tue, May 13, 2003 (2):
Wed, May 14, 2003 (3):
Thu, May 15, 2003 (4):
Mon, May 19, 2003 (1):
Tue, May 20, 2003 (2):
Wed, May 21, 2003 (3):
... etc
Projection Keyboards ... next generation?
These projection keyboards look very sci-fi but pretty cool
Happy coding to you!
|
Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :) New forum for discussion of diary entries
The homeroom at Script School is available to discuss this and other php-scripts.com diary entries. You must be an enrolled student at Script School to add comments to these diary entries. |
[back]
05/10/03 "How
to track & skip dates into the future using PHP"
[next]
Home: PHP Diary | Script School | PHP Scripts | TD Scripts.com
Copyright 1999-2003 php-scripts.com Last Modified 05/10/03 10:44