SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generating the next 60 days from a certain date

    Hi,

    I want to generate the dates (day/month/year) for 60 weekdays from a certain date so as to have the following:

    Code:
    30/04/07 - Day 1 - Something to happen on Day 1
    01/05/07 - Day 2 - Something to happen on Day 2
    02/05/07 - Day 3 - Something to happen on Day 3
    03/05/07 - Day 4 - Something to happen on Day 4
    04/05/07 - Day 5 - Something to happen on Day 5
    ...
    However, if the day is on a weekend I want to skip on to the next day.

    and so on.

    Can anybody help?

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $today 
    time();
    //create the loop
    for($i=1;$i<=60;$i++){
       
    $date strtotime("+$i day"$today);
       if(
    date('D'$date) != 'Sat' && date('N'$date) != 'Sun')){
            print 
    date('d/m/Y'$date) . " ........ Day $i ........ Something to happen on Day $i<br />"
       } 
    }
    ?>
    Last edited by Kailash Badu; Apr 30, 2007 at 09:07. Reason: fixed an error

  3. #3
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for the quick reply, Sundays are still showing up though.

    EDIT:
    Nevermind, I have it.

  4. #4
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sundays are showing up because the code i gave you is slightly messed up. Probably you know what i mean. Ok Let me edit it.

  5. #5
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for that, how would you skip a list of dates, like user entered holidays and such.

    If there was an array of holidays such as:
    PHP Code:
    $holidays = array('07-05-2007''24-05-2007''02-07-2007'); 
    How would you skip them aswell?

  6. #6
    SitePoint Zealot
    Join Date
    May 2005
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to make it clear what I'm doing here. I'm trying to set up a system that generates a course schedule for 60 days. However, I don't want to include weekends or user-entered holidays.
    PHP Code:
    // Set timestamp for start date
    $start explode('/'$_GET['start_date']);
    $startdate mktime(000$start[0], $start[1], $start[2]);
    $holidays = array('07-05-2007''24-05-2007''02-07-2007');

    // Set day var to 0
    $day        0;

    // Create the table
    echo "<table>\n";

    // Create the loop
    for ( $i 0$i 60$i++ )
    {
       
    // Add one day to timestamp
       
    $date strtotime("+$i day"$startdate);
       
    $type 'normal';

       
    // Loop through holidays and set type if date matches
       
    foreach ($holidays as $holiday)
       {
          if ( 
    date('d-m-Y'$date) == $holiday)
          {
             
    $type 'holiday';
          }
       }

       
    // Check day is not a Weekend
       
    if ( date('D'$date) != 'Sat' && date('D'$date) != 'Sun' && $type != 'holiday' )
       {
          
    $day += 1;
          
    // Print table row
          
    echo "\t<tr>\n"
          
    ."\t\t<td>date('l, jS M Y', $date)</td>\n"
          
    ."\t\t<td>Day $day</td>\n"
          
    ."\t\t<td>Something to happen on Day $day</td>\n"
          
    ."\t\t<td><a href='?edit=true&amp;id=$day'>Edit</a></td>\n"
          
    ."\t</tr>\n";
       }

    }
       
    // Close table
       
    echo "</table>\n"
    The schedule will then be added to a database with some auto-generated text for the entry.
    Code:
    mysql_query("INSERT INTO `course_schedule` SET date='$date', day_number='$day', day_details='$day_details'")
    Then later, when an administrator wants to add a holiday to the system, it re-runs the generator updates the dates for each day and aligns the correct date with the correct day.

    At the moment I'm having problems keeping the day count to 60 after I remove weekends and holidays from the loop the day count goes to 42.

    How do I keep the day count to 60, keep in mind that I'll never know the amount of holidays.

    Ii was thinking about using count($holidays) and adding that to the for loop:
    PHP Code:
    for ( $i 0$i 84+count($holidays); $i++ )
    {
       ...

    I used 84 to bring the count to 60 after excluding 12 weekends. So, 60 + 24

    Any ideas welcome, also anyways to shorten the amount of code is definitely welcome.

    Thanks,
    Eoghan.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •