0

I am trying to add days and/or weeks to a date that is being pulled from a database. All I am getting is the 12-31-1969 default date when it cannot output correctly. Here is my code:

$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'));

I have also tried multiplying the days times the $feedSchedule variable and replacing week(s) with day(s).

n_starnes
  • 376
  • 1
  • 6
  • 20

2 Answers2

2

6-25-2013 is not a valid date format. Try YYYY-MM-DD

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • The date format I am using works fine and for the purpose it is serving. I have used it in the past and on previous lines of code with this application. – n_starnes Jun 25 '13 at 18:06
  • @n_starnes But as you have observed, it doesn't work as an input format for `strtotime()`. You don't have to switch the format globally, but you will need to modify it for use with `strtotime()`. – George Cummins Jun 25 '13 at 18:07
  • doing it as you suggest displays: 1969-Dec-Wed I need accurate date information not day of the week. – n_starnes Jun 25 '13 at 18:15
0

Here is code that will work and account for the invalid 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;

returns

07-09-2013
amaster
  • 1,915
  • 5
  • 25
  • 51
  • Sorry, I forgot to put the semicolon on the third line... just tried it in PHP sandbox and all works! – amaster Jun 25 '13 at 18:21
  • I placed the semicolon there because I noticed it as well. It still does not work. – n_starnes Jun 25 '13 at 18:23
  • try this before running the conversion `echo $lastFeed;` and let me know exactly what you are getting. – amaster Jun 25 '13 at 18:25
  • I am running this without a conversion. $lastFeed when echoed shows the correct format of 6-25-2013 and then $lastFeed is then used in your function. Once the function is run it shows the date as 12-31-1969. $feedschedule varies each time it is used. On one animal it is 2 on the other it can be 10. – n_starnes Jun 25 '13 at 18:30
  • I did put in the date and the raw number instead of a variable and it returned it just fine. – n_starnes Jun 25 '13 at 18:33
  • can you edit your question to include all of your code including mysql query... please remember security though and do not include your database credentials... I believe you may have a typo with a variable somewhere if it works when you define the variable hard coded vs defining it with your mysql query – amaster Jun 25 '13 at 18:36
  • I echoed the $correctedDate after the conversion in the function and it is showing a formated 6-25-2013 from the variable as 2013-006-25. Any ideas? – n_starnes Jun 25 '13 at 18:36
  • I have verified that I am pulling the MySQL data correctly and echoed/printed it onto the webpage successfully. It is going through your function but is not adding the weeks to the date successfully. – n_starnes Jun 25 '13 at 18:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32354/discussion-between-amaster507-and-n-starnes) – amaster Jun 25 '13 at 18:41