Lewis Smith Lewis Smith - 11 months ago 46
PHP Question

Add a number of weeks using a variable as the week amount php

I am trying to use the code seen below to add a specific number of weeks to a starting date. The variable "weekdate" refers to the number of weeks to be added, this can range from 1 all the way up to ∞.
I have taken 1 from each of the dates for a reason that is right but hard to explain but this is not the issue.

What is the mistake I have made here?

$weekdate = $row['Week'];
//sets weekdate = 1 -> any number
$weekdate = $weekdate - 1;
//take 1 from that start number
$newdate = date($startdate, strtotime("+" . $weekdate . " Week"));
//add weekdate number of weeks to the start date.
echo $newdate . "<br>";
//echo out.


24-10-2016 start date,

+1 week

result output: 31-10-2016

Answer Source

try this code. define start date as your variable. i don't have so i put it manually.

$weekdate = 2;
//sets weekdate = 1 -> any number
$weekdate = $weekdate - 1;

$start_date = "2016-10-24";  
$date = strtotime($start_date);
$date = strtotime('+'.$weekdate.' weeks', $date);
echo date('d-m-Y', $date);