简体   繁体   中英

How to insert an irregular calendar events in mysql using PHP

I'm trying to enter events that occur over the same interval (Weekly, Bi-Monthly, Quarterly, Yearly). The script I'm showing is that of monthly..(If I can get that right, I can make an adaptation for the other intervals).

Events are entered along with the correct Week Number in which they occur.. But the following problem occur:

--- How can I make sure that months that has less than 31 days do not have events entered for them ? ----- If an event occurs on every 1st and 3rd friday (or whichever one), or last thursday of the month. How can I tweak the script below do do that.

Thank you.

A Sample of what I'm getting from mysql, when I insert...

+----+------+------+------------------+-----------+------------+----------+
| day| month| year | eventname        | eventtime | eventplace | eventweek|
+----+------+------+------------------+-----------+------------+-----------
|  1 |    2 | 2011 | Social Gathering | 12PM      | Room       |   05     |
|  8 |    2 | 2011 | Social Gathering | 12PM      | Room       |   06     |
|  15|    2 | 2011 | Social Gathering | 12PM      | Room       |   07     |
|  22|    2 | 2011 | Social Gathering | 12PM      | Room       |   08     |
|  29|    2 | 2011 | Social Gathering | 12PM      | Room       |   09     |
+----+------+------+------------------+-----------+------------+----------+

PHP Code

<?php 
    if(isset($_POST['myform'])){

             $day   =   array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
             $month =   array('1','2','3','4','5','6','7','8','9','10','11','12');              
             $year  =   array('2011', '2012','2013', '2014', '2015', '2016');   

   $startday   =  mysql_real_escape_string($_POST['day']);
   $eventplace =  mysql_real_escape_string($_POST['eventplace']);
   $eventname  =  mysql_real_escape_string($_POST['eventname']);
   $eventtime  =  mysql_real_escape_string($_POST['eventtime']);

            for($i=0; $i<count($year); $i++){
                for($j=0; $j<count($month); $j++){
                    for($k=$startday; $k<count($day); $k = $k + 7){
                        $date = mktime(0,0,0,$month[$j],$k,$year[$i]); 
                            $week = date('W', $date) ;
      $query = mysql_query(" INSERT INTO caldemo(day, month, year, eventname, eventtime, eventplace, eventweek)
                             VALUES ('".intval($k)."', '".intval($month[$j])."', '".intval($year[$i])."', '".$eventname."', '".$eventtime."', '".$eventplace."', '".intval($week)."' )")or die(mysql_error()) ;
                  }
                }
             } 
          }
?>

Simple php solution:

function setDatePeriod($months=1, $date='')
{
    if(!$date) $date = date('Y-m-d');
    $d = new DateTime( $date );
    $d->modify( "+{$months} months" );
    return $d->format('Y-m-d');
}
echo setDatePeriod(3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM