php strtotime中遇到的一个非常好的数值[重复]
问题描述:
This question already has an answer here:
- Adding days to $Date in PHP 9 answers
how to solve this error?
$timeFirst = strtotime('2011-05-12 18:20:20');
$timeSecond = strtotime('2011-05-13 18:20:20');
$differenceInSeconds = $timeSecond - $timeFirst;
$hour=$differenceInSeconds/60/60;
$days=$hour/24;
$days=ceil($days);
$data=array();
for($i=1; $i<=7; $i++){
if($i==1){
$next_travel_date=date('d-m-Y',$timeFirst);
$next_endDate=date('d-m-Y',$timeSecond);
}else if($i>1){
$string=" + $days days";
$date1=strtotime($string, $next_travel_date);
$date2= strtotime($string, $next_endDate);
$next_travel_date=date('d-m-Y',$date1 );
$next_endDate=date('d-m-Y',$date2);
}
$data['travel_date']=$next_travel_date;
$data['end_date']=$next_endDate;
echo $data['travel_date'].' - '.$data['end_date'].'<br>';
}
Notice: A non well formed numeric value encountered in D:\xamp\htdocs\timetest.php on line 19
Notice: A non well formed numeric value encountered in D:\xamp\htdocs\timetest.php on line 20 02-01-1970 - 02-01-1970
line 19:
$date1=strtotime($string, $next_travel_date); $date2= strtotime($string, $next_endDate);
</div>
答
As I understand, for adding some days or time in any date, strtotime takes first parameter as date string and some interval with it.
So code should be :
$string = " + $days days";
$date1 = strtotime($next_travel_date . $string);
$date2 = strtotime($next_endDate . $string);
$next_travel_date = date('d-m-Y', $date1);
$next_endDate = date('d-m-Y', $date2);