1

我正在尝试使用下面的代码将事件输入到MySQL表中PHP。当我运行它时,它没有给出任何错误,并且没有将行插入到MySQL表中。我在代码中哪里出错了?

还有当我改变

$date = strtotime("third friday of $month[$j] $year[$i]");

$date = mktime(0,0,0,$month[$j],$k,$year[$i]);

它适用于所有日子,但我正在寻找每个月的第三个星期五..

<?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   = $_POST['day'];
         $eventplace = $_POST['eventplace'];
         $eventname  = $_POST['eventname'];
         $eventtime  = $_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 = strtotime("third friday of $month[$j] $year[$i]");

                        $week = date('W', $date) ;

  $query = mysql_query(" INSERT INTO caldemo(day, month, year, eventname, eventtime, eventplace, eventweek)
                         VALUES ('".$k."', '".$month[$j]."', '".$year[$i]."', '".$eventname."', '".$eventtime."', '".$eventplace."', '".$week."' )")or die(mysql_error()) ;
              }
            }
         } 
      }
?>   


<form name="theform" method="post" action="caldemo.php">
<table>
<tr>
<tr>
<td>Event Venue:</td>
<td><input type="text" name="eventplace" size="50"></td>
</tr>
<tr>
<td>Event Name:</td>
<td><input type="text" name="eventname" size="50"></td>
</tr>
<tr>
<td>Event Time:</td>
<td><input type="text" name="eventtime" size="50"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Send" name="myform">
</form>
4

3 回答 3

3

尝试

for($j=0; $j<count($month); $j++){

  $date = strtotime("+2 week friday $month[$j] $year[$i]");
  $week = date('W', $date) ;
  $k = date('d', $date) ;

  $query = mysql_query(" INSERT INTO caldemo 
    (day, month, year, eventname, eventtime, eventplace, eventweek)
    VALUES 
    ('".$k."', '".$month[$j]."', '".$year[$i]."', '".$eventname."', 
    '".$eventtime."', '".$eventplace."', '".$week."' )")
  or die(mysql_error()) ;
}

删除了第三个for循环。将参数更改为strtotime并使用day$k在查询中返回。

于 2012-10-31T12:26:35.693 回答
1

请用

strtotime("$month[$j] $year[$i] third friday");

哪里$month[$j] 应该是月份名称december$year[$i]应该是年份2012

谢谢

于 2012-10-31T12:35:18.537 回答
-1

//showDay('一月', 2017, '星期五', 3);
showDay('一月', 2017, '星期五', '第三','文本');

函数 showDay($month, $year, $day, $count, $type=''){

      $list = array(1=>'first',2=>'second',3=>'third',4=>'fourth',5=>'fifth');
      $dayname = $list[$count];
      if($type=='text') $dayname = $count;

      return date('d', strtotime($month . ' ' . $year . ' ' . $dayname .' '.$day));
    }
于 2017-09-07T09:47:51.770 回答