0

I want to change date var when x days have passed

For instance:

Today is 21.12.16 - $date = '23.12.16'

Tomorrow is 22.12.16 - $date = '23.12.16'

When it's 23.12.16 - $date = '25.12.16'

Her's the code I got so far. Hope this will make some sense

   $date            = "2016-12-21"; //** will describe this lower
   $days_passed = date_create()->diff(date_create($date))->days;
if ($days_passed >= 2){
    $new_date = date('d.m.y', strtotime("+2 days"));
} else{
    $new_date = $date;
}

This works ok if I just want to do it once

**I need to change this var every 2 days. I understand that i can write it to a Database or to a .txt. But there sure is a way to do this just by php

P.S. sorry for my bad English.

  • 4
    http://stackoverflow.com/questions/18140766/check-how-many-days-were-passed-since-the-last-update-in-php – Daimos Dec 21 '16 at 09:35
  • if(date("Y.m.d") == $date){ $date = date("Y.m.d", strtotime($date. ' + 2 days')); } – Rahul Dec 21 '16 at 09:36
  • 1
    Possible duplicate of [How to get the current date and time in PHP?](http://stackoverflow.com/questions/470617/how-to-get-the-current-date-and-time-in-php) – Synchro Dec 21 '16 at 10:50

1 Answers1

0

Here's what I came up with:

$date           = '2016-12-01';  //Your script start date, you wont need to change this anymore
$everyxdate     = 10; // once x days to add x to $date
$days_passed    = date_create()->diff(date_create($date))->days; // passed days from start of script $date

$mod_dates = (int)($days_passed / $everyxdate); // count how much cycles have passed
$daystoadd = $mod_dates * $everyxdate + $everyxdate;  // count how much days we need to add
$newdate = strtotime ("+$daystoadd day" , strtotime ( $date ) ) ; // Add needed day count to starting $date 
$newdate = date ( 'd.m.y' , $newdate ); // Format date the way you want

Hope this will help some one who has the same task I had.