0

我正在尝试在一个类中使用strtotimedate函数来将天数添加到由 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']);
}
?>

如果我打印 post 变量,我可以确认它们正在被发送,但我无法弄清楚为什么我没有从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 回答