0

I have a calendar and I'm adding recurring events to it, My problem is that the user can set a "repeat for x amount of months" variable, this variable is my $repeat_value and if it exceeds 12 i need to handle it in a way that the date year increases by 1, and i should set the month to 1, then add remaining months... how can i do this?

my code is setup like this:

for ($i = 1; $i < $repeat_value; $i++)
{       
    if ($repeat_type == "month")
    {

        if ($i > 12) //month is greater then 12, we must increase the year +1 and set month to jan
        {
            //please help!
        }
        else                
        {

            $t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' months'));
            $t_temp_end   = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' months'));                  
        }

        $database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end  . "', '" . $url . "');");                       
    }
    else if ($repeat_type == "year")
    {
        $t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' years'));
        $t_temp_end   = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' years'));           
        $database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end  . "', '" . $url . "');");                                                       
    }
}   

I hope this makes sense, help appreciated!

Dean
  • 499
  • 6
  • 13
  • 34

1 Answers1

0

The fact that there is more than 12 months has absolutely no importance.

Simply put:

for ($i = 1; $i < $repeat_value; $i++)
{       
    if ($repeat_type == "month")
    {
        $t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' months'));
        $t_temp_end   = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' months'));
        $database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end  . "', '" . $url . "');");                       
    }
    else if ($repeat_type == "year")
    {
        $t_temp_start = date('Y-m-d h:m:s',strtotime($s_start . '+ ' . $i . ' years'));
        $t_temp_end   = date('Y-m-d h:m:s',strtotime($s_end . '+ ' . $i . ' years'));           
        $database->justquery("INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end  . "', '" . $url . "');");                                                       
    }
}

You can try this:

// these two lines produce exactly the same result
print date('Y-m-d h:m:s',strtotime('today + 14 months'));
print date('Y-m-d h:m:s',strtotime('today + 1 year + 2 months'));

Also, I thought you could try this simpler/shorter way of doing what you want:

$db_query = '';

for ($i = 1; $i < $repeat_value; $i++)
{
    $t_temp_start = data('Y-m-d h:m:s', strtotime($s_start.' + '.$i.' '.$repeat_type.'s'));
    $t_temp_end = $t_temp_start + $s_end - $s_start;
    db_query .= "INSERT INTO tbl_events (`title`, `start`, `end`, `url`) VALUES ('" . $event_name . "', '" . $t_temp_start . "', '" . $t_temp_end  . "', '" . $url . "');";
}

if (!empty($db_query))
    $database->justquery($db_query);

Which potentially saves you lots of database queries (which should never be placed inside loops), and makes your code easier to read (by saving you a conditional statement). It also enables you to accept repeats each year, month, but also week, day, hour and so on without changing a single byte of your code.

In a more general point of view, you should notice when you have an if() ... else conditional statement where the else is almost exactly a repetition of the if. It is often a sign that there is room for improvement in your code.

And one last word: be careful to SQL injections.

Community
  • 1
  • 1
Jivan
  • 21,522
  • 15
  • 80
  • 131