I am trying use strtotime and date functions within a class to add a number of days to a date consisting of 3 post variables, $month,$day,$year and I don’t know what i’m doing wrong. The date is submitted from a form generated by a function and passed to 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']);
}
?>
If I print the post variables, I can confirm they are being sent, but I can’t quite figure out why i’m not getting return data from setDays().
Any ideas?
EDIT:
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;
}
You should try this:
As I know,
strtotime()can be used for adding a number of days like this: