我正在尝试在一个类中使用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;
}
答案 0 :(得分: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");
答案 1 :(得分: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']);
}
?>