添加日期变量日期

时间:2013-06-25 17:58:51

标签: php mysql date strtotime

我正在尝试将日期和/或周数添加到从数据库中提取的日期。我得到的只是无法正确输出的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变量的天数乘以及将星期替换为星期几。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

以下代码可用于处理无效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