2012-10-31 108 views
1

我在下面的代碼中使用PHP來將事件輸入到MySQL表中。當我運行它時,它不會發生錯誤,並且沒有行插入到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> 

回答

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

+0

@MC_delta_T感謝您指出'strtotime('2012年1月第一個星期日'))'會給出錯誤的結果。 – air4x

1

請使用

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

$month[$j]december$year[$i]應該是一個月的名稱應該是像今年2012

感謝

-1

// showDay( '一月',2017年, '星期五',3); showDay('january',2017,'friday','third','text');

 

function 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)); 
    } 
相關問題