0

strtotimeクラス内でと関数を使用dateして、3 つの投稿変数で構成される日付に日数を追加しようとしていますが、$month,$day,$year何が間違っているのかわかりません。関数によって生成されたフォームから日付が送信され、 に渡されstrtotimeます。

<?php
//Set vars
$month=$_POST['month'];
$day=$_POST['day'];
$year=$_POST['year'];

//Initalize class
$init=new Some_Class();
$init->setVars($month,$day,$year);

class Some_Class{

    private $someVar;
    private $month;
    private $day;
    private $year;

    public function setVars($var1,$var2,$var3) {
        $this->month=$var1;
        $this->day=$var2;  
        $this->year=$var3;
    }

    function __construct() { 

    }


    function setDate(){
        $start=date("Y")-50;
        $end=date("Y"); 

        $months=array('','January','February','March','April','May',
        'June','July','August', 'September','October','November','December');

        // Month dropdown
        $this->someVar='<select name="month">';

        for($i=1;$i<=12;$i++){
           $this->someVar.="<option value='".str_pad($i, 2, '0', STR_PAD_LEFT)."'>$months[$i]</option>";
        }
        $this->someVar.="</select> ";

        // Day dropdown
        $this->someVar.='<select name="day">';
        for($i=1;$i<=31;$i++){
           $this->someVar.="<option $selected value='".str_pad($i, 2, '0', STR_PAD_LEFT)."'>$i</option>";
        }
        $this->someVar.="</select> ";

        // Year dropdown
        $this->someVar.='<select name="year">';

        for($i=$start;$i<=$end;$i++){      
          $this->someVar.="<option value='$i'>$i</option>";
        }
        $this->someVar.="</select> ";

        return $this->someVar;
    }

    function setDays(){
        $this->someVar['date']=strtotime(implode('-', array($this->year,$this->month,$this->day)));
        $this->someVar['new_date']=strtotime('+42 day',$this->someVar['date']);
        return $this->someVar;  
    }
}

$setDate=$init->setDate();?>
<form action="<?php $_SERVER['REQUEST_URI'];?>" method="post">

  <?php echo $setDate;?>

  <input type="submit" value="submit" name="Submit"/> 
</form>
<?php 
if(isset($month,$day,$year)){
    $setDays=$init->setDays();
    echo date('M d, Y',$setDays['new_date']);
}
?>

ポスト変数を印刷すると、それらが送信されていることを確認できますが、 からの戻りデータを取得できない理由がわかりませんsetDays()

何か案は?

編集:

function setDays() {
        $this->someVar = array();
        $this->someVar['date']=strtotime(implode('-', array($this->year,$this->month,$this->day)));
        $this->someVar['new_date']=strtotime("+42 day",$this->someVar['date']);
        return $this->someVar;  
    }
4

2 に答える 2

1

これを試してください:

$this->someVar['date']=strtotime(implode('-', array($this->year,$this->month,$this->day)));
$this->someVar['new_date']=$this->someVar['date'] + 42 * 24 * 60 * 60;

私が知っているstrtotime()ように、次のように日数を追加するために使用できます。

$date = strtotime('Y-m-d', time() . " +42 day");
于 2012-11-02T07:41:08.720 に答える
0

どういうわけかエラーが発生するため、コードの一部を再構築します。ほとんどの場合、変数someVarを配列として使用する前に、配列として宣言する必要があります。

 <?php
 //Set vars
 $month=$_POST['month'];
 $day=$_POST['day'];
 $year=$_POST['year'];

 //Initalize class
 $init=new Some_Class();
 $init->setVars($month,$day,$year);

 class Some_Class{

     private $someVar;
     private $month;
     private $day;
     private $year;

     public function setVars($var1,$var2,$var3) {
         $this->month=$var1;
         $this->day=$var2;  
         $this->year=$var3;
     }

     function __construct() { 

     }


     function setDate(){
         $start=date("Y")-50;
         $end=date("Y"); 

         $months=array('','January','February','March','April','May',
    'June','July','August', 'September','October','November','December');

         // Month dropdown
         $this->someVar='<select name="month">';

         for($i=1;$i<=12;$i++)
         {
            $this->someVar.="<option value='".str_pad($i, 2, '0', STR_PAD_LEFT)."'>$months[$i]</option>";
         }
         $this->someVar.="</select> ";

         // Day dropdown
         $this->someVar.='<select name="day">';
         for($i=1;$i<=31;$i++)
         {
            $this->someVar.="<option value='".str_pad($i, 2, '0', STR_PAD_LEFT)."'>$i</option>";
         }
         $this->someVar.="</select> ";

         // Year dropdown
         $this->someVar.='<select name="year">';

         for($i=$start;$i<=$end;$i++)
         {      
           $this->someVar.="<option value='$i'>$i</option>";
         }
         $this->someVar.="</select> ";

         return $this->someVar;
     }

     function setDays(){
    $array_date = array();
         $array_date['date'] = implode('-', array($this->year,$this->month,$this->day));echo implode('-', array($this->year,$this->month,$this->day)); var_dump($array_date);
         $array_date['new_date'] = strtotime($array_date['date'].' +42 day');
         return $array_date;  
     }
 }

 $setDate=$init->setDate();?>
 <form action="<?php $_SERVER['REQUEST_URI'];?>" method="post">

   <?php echo $setDate;?>

   <input type="submit" value="submit" name="Submit"/> 
 </form>
 <?php 
 if(isset($month,$day,$year)){
     $setDays=$init->setDays();
     echo date('M d, Y',$setDays['new_date']);
 }
 ?>        
于 2012-11-02T07:56:52.973 に答える