Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting start and end date of a PHP DatePeriod object?

Tags:

date

php

datetime

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; }
}
like image 534
gremo Avatar asked May 30 '12 14:05

gremo


2 Answers

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);
like image 163
Boby Avatar answered Oct 07 '22 14:10

Boby


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.

like image 25
kellanburket Avatar answered Oct 07 '22 14:10

kellanburket