Avinash
Avinash

Reputation: 2005

Get all datetimes between two datetime based on interval

Lets take two datetimes 2013-07-22 and 2013-07-28. The datetimes between these two datetimes are 2013-07-23, 2013-07-24, 2013-07-25, 2013-07-26, 2013-07-27, 2013-07-28.

I am able to get this much to work using php datetime.

 $interval = DateInterval::createFromDateString('1 days');
 $period = new DatePeriod($startDate, $interval, $endDate, DatePeriod::EXCLUDE_START_DATE);

What I Require

I have another variable $interval which can take values 1,2,3.... If $interval = 2 then $period will only contain 2013-07-24, 2013-07-26 , 2013-07-28. Likewise $interval = 3 then $period will only contain 2013-07-25, 2013-07-28.

How can do this?

Upvotes: 0

Views: 94

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33521

You can create any interval with that variable, just write:

$interval = DateInterval::createFromDateString("$interval days");

instead and you are set.

Upvotes: 3

Related Questions