-4

I need to make some operations on date. From the DB script is reading a date and frequency of actions (ex. 21 days), and should calculate when will be the next action since actual date. I've tried to do this in some ways (it's hard to me to write it in English, so below you can find the code).

    //$unix_on - date from DB
    //$devices[$i]['freq'] - frequency of actions
    $unix_on=strtotime($devices[$i]['date_on']);
    $unix_today=strtotime(date('Y-m-d'));
    $actions=($unix_today-$unix_on)/(86400*$devices[$i]['freq']);
    $b=explode(".", $actions);
    $a='0'.'.'.$b['1'];
    $f=$a*$devices[$i]['freq'];
    $d=$unix_today+($f*86400);
    $e=date("Y-m-d",$d); 

But it not work's fine - there are errors in calculations, and I don't know why.

kacper
  • 370
  • 2
  • 6
  • 13
  • Please use the search function. [How to add time](http://stackoverflow.com/search?q=add+time+php) has been asked and answered countless times before – Gordon Mar 10 '13 at 16:03

2 Answers2

1

strtotime is a lot more powerful than you're using it for. Try this:

$e = date("Y-m-d",strtotime($devices[$i]['date_on']." +".$devices[$i]['freq']."days"));
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1
$dateOn = new DateTime($devices[$i]['date_on']);
$frecuency = new DateInterval('P' . $devices[$i]['freq'] . 'D'); // Period of x Days

$dateOn->add($frequency);

$e = $dateOn->format('Y-m-d');

See http://es2.php.net/manual/en/datetime.construct.php, http://es2.php.net/manual/en/datetime.add.php and http://es2.php.net/manual/en/dateinterval.construct.php

Maks3w
  • 6,014
  • 6
  • 37
  • 42
  • Thanks a lot for your help. Code works, but only for the first action - it doesn't showing next date since actual date... – kacper Mar 10 '13 at 22:44