Results 1 to 2 of 2

Thread: Date/Time Loop ...

  1. #1
    Join Date
    Nov 2008
    Posts
    52
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Date/Time Loop ...

    Hello:

    I am trying to create an appointment table and am having problems. I have a loading facility that I need to create an appointment book for. The theory is simple, but where I am having problems is creating a loop for adding to the table. Basically I need a to create a form where I enter the start date, start time along with end date and end time. Let's say the facility loads every day and has hourly appointments starting at 0700 hrs and ends at 1800 hrs. I would like to enter the start date, let's say April 01, 2011 and end date of September 1, 2011. Loop and create records of each date with the hour. Can anyone help me with this please. I have included a sample of the output in my mysql table:


    tblName02
    04/01/2011 07:00:00
    04/01/2011 08:00:00
    04/01/2011 09:00:00
    04/01/2011 10:00:00
    04/01/2011 11:00:00
    04/01/2011 12:00:00
    04/01/2011 13:00:00
    04/01/2011 14:00:00
    04/01/2011 15:00:00
    04/01/2011 16:00:00
    04/01/2011 17:00:00
    04/01/2011 18:00:00

    04/02/2011 07:00:00
    04/02/2011 08:00:00
    ...

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    <?php
    $end_date  ="September 1, 2011 18:00:00";
    $end_date  =date("YmdHis", strtotime("$end_date"));
    $end_date1=$end_date;
                $end_date=str_replace(" ","",$end_date);
                $end_date=str_replace(":","",$end_date);
                $end_date=str_replace("-","",$end_date);
                $y1_end=substr($end_date,0,4);
                $m1_end=substr($end_date,4,2);
                $s1_end=substr($end_date,12,2);
                $d1_end=substr($end_date,6,2);
                $h1_end=substr($end_date,8,2);
                $min1_end=substr($end_date,10,2);
    $time_end="$m1_end/$d1_end/$y1_end $h1_end:$min1_end:$s1_end<br>";
    
    $start_date="April 1, 2011 07:00:00";
    $hour='0';
    $day='0';
    $counter='0';
    while ($hour>=0){
    if ($hour=='12') {$hour='0';$day++;}
    $date=date("YmdHis", strtotime("$start_date + $hour hour + $day days"));
    $hour++;$counter++;
                $date=str_replace(" ","",$date);
                $date=str_replace(":","",$date);
                $date=str_replace("-","",$date);
                $y1=substr($date,0,4);
                $m1=substr($date,4,2);
                $s1=substr($date,12,2);
                $d1=substr($date,6,2);
                $h1=substr($date,8,2);
                $min1=substr($date,10,2);
    $time="$m1/$d1/$y1 $h1:$min1:$s1<br>";
                echo "$time";
    if ($time_end=="$time") break;
    if ($counter>5000) break;
    }
    ?>
    I wasn't sure what to use for the while statement while ($hour>=0){ so I just put it in an infinite loop to break when the desired date is reached.

    EDIT: I'm not sure what dates you are working with, so I added the following line to act as a safe guard in case there is an error in the start or end date input so as to keep from generating a loop that goes on till the end of time, which in php is somewhere around January 19, 2038:

    if ($counter>5000) break;
    Last edited by james438; 04-21-2011 at 05:01 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

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
  •