我试图将几天和/或几周添加到从数据库中提取的日期中。我得到的只是它无法正确输出的1969年12月31日的默认日期。这是我的代码:

$lastFeed = "6-25-2013"; //pulled from database last feed date

$feedSchedule = "2"; //pulled from database number of weeks.

$nextFeeding = date("m-d-Y", strtotime($lastFeed . ' + ' . $feedSchedule. ' week'));


我还尝试将天数乘以$ feedSchedule变量,然后将周替换为天。

最佳答案

这是将起作用并说明无效的Date Time String的代码

function nextFeeding($lastFeed,$feedSchedule){
  //fix date format
  $correctedDate = explode("-",$lastFeed);
  //pad month to two digits may need to do this with day also
  if($correctedDate[0] < 10 && strlen($correctedDate[0])!==2){
    $correctedDate[0] = "0".$correctedDate[0];
  }
  $correctedDate = $correctedDate[2]."-".$correctedDate[0]."-".$correctedDate[1];
  //get the next feeding date
  $nextFeeding = date("m-d-Y", strtotime($correctedDate . ' + ' . $feedSchedule. ' week'));
  //return value
  return $nextFeeding;
}

$lastFeed = "6-25-2013"; //pulled from database last feed date

$feedSchedule = "2"; //pulled from database number of weeks.

$nextFeeding = nextFeeding($lastFeed,$feedSchedule);
echo $nextFeeding;


退货

07-09-2013

09-20 03:27