簡體   English   中英

PHP的strtotime和日期問題

[英]php strtotime and date issues

我正在嘗試在一個類中使用strtotimedate函數向由3個后置變量$month,$day,$year組成的日期中$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;  
    }

您應該嘗試這樣:

$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");

我重構了您的一些代碼,因為它以某種方式給出了錯誤。 最有可能將變量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']);
 }
 ?>        

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM