【问题标题】:Using DateTime to create an interval of time and divide it into days使用 DateTime 创建时间间隔并将其划分为天
【发布时间】:2014-04-26 18:24:36
【问题描述】:

我发布开始日期和时间,以及结束日期和时间,例如:

$_POST['start_date'] //this might be 27:04:2013
$_POST['start_time'] //this might be 16:30
$_POST['end_date'] //this might be 29:04:2013
$_POST['end_time'] //this might equal 22:30

我想为发布的时间间隔延伸的每一天创建一个对象数组,这是一个 DateInterval 对象吗?如果是这样,那么在上面发布的值中,我想获得一个包含以下内容的数组

[0] 27:04:2013 16:30:00 27:04:2013 23:59:59
[1] 28 04:2014 00:00:00 28 04:2014 23:59:59 
[2] 29:04:2014 00:00:00 29:04:2014 22:30:00

【问题讨论】:

  • 这将是一个 DateInterval 对象的数组,如果你想要的是... DateInterval 对象的数组。我们不能告诉你你想要什么。另外,你试过什么?你在哪里卡住了?
  • 那么,给定一个开始日期/时间和一个结束日期/时间,您想创建一个数组,其中每个元素代表每一天,包括开始日期和结束日期?
  • 是的,我需要 DateInterval 对象上的数组。
  • 是的,达拉赫。对于帖子持续时间延长的每一天,我的数组中的每个元素都有日期以及开始和结束时间
  • 我编辑了我希望得到的数组的格式,如果我混淆了,对不起

标签: php datetime dateinterval


【解决方案1】:
$start = DateTime::createFromFormat('d:m:Y H:i', '27:04:2013 16:30');
$end   = DateTime::createFromFormat('d:m:Y H:i', '29:04:2013 22:30');
$diff  = $start->diff($end);
$pad   = ($start->format('His') > $end->format('His')) ? 2 : 1;
$days  = $diff->d + $pad;
for ($i = 1; $i <= $days; $i++) {
    if ($i === 1) {
        printf("%s %s<br>", $start->format('d:m:Y H:i:s'), $start->format('d:m:Y 23:59:59'));
    }
    else if ($i === $days) {
        printf("%s %s<br>", $end->format('d:m:Y 00:00:00'), $end->format('d:m:Y H:i:s'));
    }
    else {
        printf("%s %s<br>", $start->format('d:m:Y 00:00:00'), $start->format('d:m:Y 23:59:59'));
    }
    $start->modify('+1 day');
}

Demo

【讨论】:

  • 这一年很奇怪。看起来像一个PHP错误。让我看看如何解决这个问题。
  • 好的。解决了它。 codepad.viper-7.com/0xzVCn 必须探索一下,看看为什么会有意想不到的结果。但我建议将来使用标准日期时间格式。您的格式只会让处理日期变得更加困难。
  • @JohnConde 您的原始代码使用了H:m 格式,而不是正确的H:i
【解决方案2】:

目前尚不清楚您是否真的想要一个DateInterval 对象或者可能是每天单独的开始/结束DateTime。无论哪种方式,以下内容都应该作为一个明智的出发点。

<?php

$start_date = '27:04:2013';
$start_time = '16:30';
$end_date = '29:04:2013';
$end_time = '22:30';

// Date input strings and generate a suitable DatePeriod
$start = DateTime::createFromFormat("d:m:Y H:i", "$start_date $start_time");
$end = DateTime::createFromFormat("d:m:Y H:i", "$end_date $end_time");
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $date) {
    // Get midnight at start of current day
    $date_start = clone $date;
    $date_start->modify('midnight');

    // Get 23:59:59, end of current day
    // (moving to midnight of next day might be good too)
    $date_end = clone $date;
    $date_end->modify('23:59:59');

    // Take care of partial days
    $date_start = max($start, $date_start);
    $date_end = min($end, $date_end);

    // Here you would construct your array of
    // DateTime pairs, or DateIntervals, as you want.
    printf(
        "%s -> %s \n",
        $date_start->format('Y-m-d H:i'),
        $date_end->format('Y-m-d H:i')
    );
}

产生以下输出:

2013-04-27 16:30 -> 2013-04-27 23:59 
2013-04-28 00:00 -> 2013-04-28 23:59 
2013-04-29 00:00 -> 2013-04-29 22:30

附录

如果您有幸能够使用 PHP 5.5.0 或更高版本,那么 DateTimeImmutable 类将使克隆/修改部分更加整洁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    相关资源
    最近更新 更多