Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Adding Dates To A Set Date
#1

[eluser]defectivereject[/eluser]
Hello again.
So following on from my last calendar issue, i've gotten stuck adding dates.
All i'm after is a simple "add 1 week onto this date" foreach time the date appears in this loop.

whereas what i am getting is one week added to the original date requested no matter how many times it inserts the line

incoming data:
date_t = Day cell clicked on the calendar //want date intervals adding to this
data_t - meeting/event details
reoccurs = 1,2,3,4,5 e.t.c. as to how many weeks/fortnights you want to book the room
time_t = simply a timeslot someone wants i.e. 9-11 or 130-230 e.t.c.

My current query is as follows
Code:
function add_calendar_events($date, $data, $dateto, $time) {
        $i = 1;
        $this->db->insert('calendar2', array(
            'date_t' => date_to_mysqldatetime($date),
            'data_t' => $data,
            'reoccurs'=> $dateto,
            'time_t' => $time                  
        ));

// The above inserts the line into the DB
//The post data['reoccurs'] is a simple 1,2,3,4,5,e.t.c. to say how many weeks the event reoccurs for, next week, 2 weeks, 5 weeks e.t.c.
// There is only one person managing this, so using MAX(id) is fine.

        $query = $this->db->query("SELECT max(id) as maxid FROM calendar2"); //selects the line id just added
        $row = $query->row_array();
        $max_id = $row['maxid'];
        
        $query2=$this->db->query("SELECT date_t, reoccurs FROM calendar2 WHERE id= $max_id");
//selects the date and occurrence factor of the line just added
        $row2 = $query2->row_array();
        $occurs = $row2['reoccurs'];       //occurrence factor
        $added_date = $row2['date_t'];  //initial date requested

        while ($i <= $occurs) {    //adds a line for each week requested
            ++$i;    
        $this->db->query(
            "INSERT INTO calendar2
            (date_t,data_t,reoccurs,time_t)
            VALUES
            (date_add('$added_date', INTERVAL 7 DAY), '$data', '$dateto', '$time')");
    }
//date_add adds the seven days on, but its only adding it to the original date.
// I expected that it would loop through each time to pull the MAX(id) and use the date of that one?
        }

I can and will add on a check eventually to see if they requested every week or 2 weeks or monthly and then running the query necessary from the form selection to add on 7, 14, or a month days.
Again most queries are done in MYSQL here purely for time factors and will be put into AR (Aside from the date ones as i can't see anything about adding dates in CI?) when done.
#2

[eluser]Kamarg[/eluser]
You need to update $added_date in your while loop. If you're wanting a php function to add dates, this is what I use.

Code:
function add_date($base_date, $days = 0, $months = 0, $years = 0){
    $cd = strtotime($base_date);
    $ret = date('m/d/Y', mktime(0, 0, 0, date('m', $cd) + $months, date('d', $cd) + $days, date('Y', $cd) + $years));
    return $ret;
}
#3

[eluser]defectivereject[/eluser]
Changed my while loop as follows to update the date
Code:
while ($i <= $occurs) {    
            ++$i;    
        $new_date = date("Y-m-d", strtotime($new_date."+7 days"));
        $this->db->query(
            "INSERT INTO calendar2
            (date_t,data_t,reoccurs,time_t)
            VALUES
            ('$new_date', '$data', '$dateto', '$time')");    
    }

No longer using MySQL to add 7 days as it didn't anyhow (!)
and $new_date is the same value as the added_date from before




Theme © iAndrew 2016 - Forum software by © MyBB