2

As I understood from PHP manual page for DatePeriod class that it is purposed to store periods of time. I.e. interval with some point at time meaning start of the interval + optional recurrences.

With the following code I create $dp variable starting today with length of one month

$start = new DateTime();
$interval = new DateInterval('P1M');
$end = clone $start;
$end->add($interval);    
$dp = new DatePeriod($start, $interval, $end);

With following code I am printing all (at my example only one cause there are no recurrences) start dates of the period:

foreach ($dp as $d)
    var_dump($d->d);

My question is how can I get interval from $dp variable?

hakre
  • 193,403
  • 52
  • 435
  • 836
Boffin
  • 570
  • 2
  • 7
  • 21
  • Is it me or is `DatePeriod` very badly documented? I see a `format` method in the examples that isn't in the docs – Pekka Feb 12 '12 at 10:45
  • @Pekka it's you. DatePeriod returns DateTimel elements when iterating it, so it's DateTime::format() you are refering to ;) – Gordon Feb 12 '12 at 10:54

1 Answers1

2

You cannot because DatePeriod does not expose any properties. If you need the Interval, reuse it from your $interval variable.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • It's sad because now DatePeriod is no more than collection of DateTime objects automatically generated when I call DatePeriod constructor ( – Boffin Feb 12 '12 at 11:46
  • @Boffin yes, that's actually what it is :) – Gordon Feb 12 '12 at 11:50
  • @Boffin: Why does it need to be anything more than that? What were you hoping for? I've never tried to do so, so I don't know if it's even possible, but you could try extending the class and overriding the constructor to save the interval and expose it through a getter method. It may not be possible to extend it though. – FtDRbwLXw6 Feb 12 '12 at 13:35