6

How can i get the start and end date of a DatePeriod object?

$today  = new \DateTime(date('Y-m-d')); // 2012-05-30
$period = new \DatePeriod($today, new \DateInterval('P1M'), 1);

$stats = new UsageStatistics($period);

class UsageStatistics
{

    protected $period, $sentEmailCount, $autoSentEmailCount;

    public function __construct(\DatePeriod $period)
    {
        $this->period = $period;

        // Current logged in user and email repository
        $user = $this->getUser();
        $repo = $this->getEmailRepository();

        // Get the start and end date for the given period
        $startDate = ...
        $endDate   = ...

        $result = $repo->getAllSentCount($user, $startDate, $endDate);

        // Assigning object properties
    }

    public function getSentEmailCount() { return $this->sentEmailCount; }

    public function getAutoSentEmailCount() { return $this->autoSentEmailCount; }
}

4 Answers 4

5

DatePeriod only implements the Traversable interface and has no other methods to either access elements or retrieve them.

You can do something easy to get start/end dates:

$periodArray = iterator_to_array($period);
$startDate = reset($periodArray);
$endDate = end($periodArray);
7
  • So ugly you have to loop. Thanks anyway.
    – gremo
    May 30, 2012 at 15:20
  • Yeah I know, I've tried with ArrayObject but it didn't worked.
    – Boby
    May 30, 2012 at 15:23
  • 1
    Funny, right know I was looking for exactly the same. It's seems, that there exists a patch out there bugs.php.net/bug.php?id=53439 But it's from 2010, so ... don't know
    – KingCrunch
    May 30, 2012 at 18:54
  • 3
    @Boby: Updated to iterator_to_array which does the job here.
    – hakre
    May 30, 2012 at 22:27
  • Cool, didn't know this function :)
    – Boby
    May 31, 2012 at 7:15
1

I'm using PHP 5.6.9 and it seems that you can use the properties end and start to access your beginning and end DateTime objects:

$p = new DatePeriod($s, $i, $e);
$startTime = $p->start; //returns $s
$endTime = $p->end; //returns $e

The PHP documentation doesn't seem to reflect this. I did a print_r of a DatePeriod object and got the following output:

DatePeriod Object
(
    [start] => DateTime Object
        (
            [date] => 2015-06-01 00:00:00.000000
            [timezone_type] => 3
            [timezone] => America/Los_Angeles
        )

    [current] => DateTime Object
        (
            [date] => 2015-06-08 00:00:00.000000
            [timezone_type] => 3
            [timezone] => America/Los_Angeles
        )

    [end] => DateTime Object
        (
            [date] => 2015-06-08 00:00:00.000000
            [timezone_type] => 3
            [timezone] => America/Los_Angeles
        )

    [interval] => DateInterval Object
        (
            [y] => 0
            [m] => 0
            [d] => 7
            [h] => 0
            [i] => 0
            [s] => 0
            [weekday] => 0
            [weekday_behavior] => 0
            [first_last_day_of] => 0
            [invert] => 0
            [days] => 
            [special_type] => 0
            [special_amount] => 0
            [have_weekday_relative] => 0
            [have_special_relative] => 0
        )

    [recurrences] => 1
    [include_start_date] => 1
)

It seems that properties current and interval are also visible.

0
0

The solution posted by @hakre and @Boby is not correct.

The $endDate is end of the period when PERIOD % INTERVAL = 0.

All other cases $endDate will be END - PERIOD.

0
$startingDate = new DateTime($startingDay);
$startingDate->modify('previous day');
$startingDate->modify('next Sunday');
     
$endingDate   = new DateTime($endingDay);
$endingDate->modify('next day');
   
$period = new DatePeriod($startingDate, new DateInterval('P1W'), $endingDate);
1
  • Please explain your solution. Mar 28, 2021 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.