我正在尝试在一个类中使用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']);
}
?>
如果我打印 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;
}