0

In this function below i am calling a function using from_date and to_date in iteration. here for each iteration i am updating the from date value and when it comes to $to_date i am calculating the day from which it returns the date from now. for example the sharing_basis returns values like 2 weeks, 5 weeks....

for example: I am using $to_date = date("Y-m-d", strtotime("+".$sharing_basis)); to get the value of $to_date. the to_date returns date of the given number of weeks from now.

but here it is returning the same date everytime. i want to calculate $to_date based on $from_date

lets say, for first iteration

$from_date = 2016-12-31
$to_date = $to_date = date("Y-m-d", strtotime("+5 weeks"));

I want to get the day date of 5 weeks from $from_date.

How can i do this?

function createSharingdates($productId){
    $startdate = getStartdate($productId);
    $sharing_basis = getSharingBasis($productId);
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "SELECT user_id FROM orders WHERE product_id='$productId' ORDER BY order_datetime";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {        
        $user_id = $row["user_id"];
        $from_date = $startdate;
        foreach ($user_id as $userid) {
            $to_date = date("Y-m-d", strtotime("+".$sharing_basis));
            addSharingDates($product_id,$userid,$fromdate,$to_date);
            $from_date = $to_date
        }   
    } else {            
        return false;       
    }
    $conn->close(); 
}
CJAY
  • 6,989
  • 18
  • 64
  • 106

4 Answers4

2

Try this

$from_date = "2014-12-31";
echo date('Y-m-d', strtotime('+6 days', strtotime($from_date)));
Abhishek B
  • 148
  • 10
0

Shortest way would be using the DateInterval class.

<?php
$date = new DateTime();
$date->add(new DateInterval('P5W'));
echo $date->format('Y-m-d');

Outputs:

2016-02-04
Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18
0
$from_date = "2016-12-31";
$to_date = date("Y-m-d", strtotime("$from_date +5 weeks"));

this one works.

jilesh
  • 436
  • 1
  • 3
  • 13
0

The DateTime and DateInterval classes are ideal for this.

$start=date( 'Y-m-d', strtotime('2016-12-31') );

$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('P5W');

$date=new DateTime( $start, $timezone);
$future=$date->add( $interval );

echo $future->format('Y-m-d');
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46