【问题标题】:Getting start and end date of a PHP DatePeriod object?获取 PHP DatePeriod 对象的开始和结束日期?
【发布时间】:2012-05-30 14:21:06
【问题描述】:

如何获取DatePeriod 对象的开始和结束日期?

$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; }
}

【问题讨论】:

    标签: datetime date php


    【解决方案1】:

    DatePeriod 仅实现 Traversable 接口,没有其他方法可以访问或检索元素。

    您可以做一些简单的事情来获取开始/结束日期:

    $periodArray = iterator_to_array($period);
    $startDate = reset($periodArray);
    $endDate = end($periodArray);
    

    【讨论】:

    • 太丑了,你必须循环。无论如何,谢谢。
    • 是的,我知道,我尝试过使用 ArrayObject,但没有成功。
    • 很有趣,我知道我在寻找完全一样的东西。看来,那里有一个补丁bugs.php.net/bug.php?id=53439 但它是从 2010 年开始的,所以......不知道
    • @Boby: 更新为 iterator_to_array 在这里完成这项工作。
    • 酷,不知道这个功能:)
    【解决方案2】:

    我使用的是 PHP 5.6.9,您似乎可以使用属性 endstart 来访问您的开始和结束 DateTime 对象:

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

    PHP 文档似乎没有反映这一点。我做了一个 DatePeriod 对象的print_r 并得到以下输出:

    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
    )
    

    似乎属性currentinterval 也是可见的。

    【讨论】:

      【解决方案3】:

      @hakre 和@Boby 发布的解决方案不正确。

      $endDatePERIOD % INTERVAL = 0 时的周期结束。

      所有其他情况$endDate 将是END - PERIOD

      【讨论】:

        【解决方案4】:
        $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);
        

        【讨论】:

        • 请解释您的解决方案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多