0

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

$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 变量,并用天数替换周数。

4

2 回答 2

2

6-25-2013不是有效的日期格式。尝试YYYY-MM-DD

于 2013-06-25T18:03:52.913 回答
0

这是可以工作并解释无效日期时间字符串的代码

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
于 2013-06-25T18:15:25.457 回答