如何使用PHP Carbon格式化日期范围?

时间:2019-10-31 10:22:58

标签: php php-carbon

PHP Carbon中是否有任何内置方法可以格式化以下日期范围?

$start_date = '2020-05-20';
$end_date = '2020-05-30';

// expected output 'May 20th-30th, 2020'

$start_date = '2019-10-24';
$end_date = '2019-11-02';

// expected output 'October 24th-November 2nd, 2019'

$start_date = '2019-12-25';
$end_date = '2020-01-03';


// expected output 'Desember 25th, 2019 - January 3rd, 2020'

还是我应该编写自己的逻辑来实现这一目标?

5 个答案:

答案 0 :(得分:1)

@jspit answer的变化形式,也允许每个案例使用两个相同的日期和特定的分隔符。

        $formats = $formats ?? [
          '' => ['F j, Y','F j, Y', ' - '], //default
          'Y' => ['F j','F j, Y', ' - '],   // Y equal
          'Ym' => ['F j','j, Y', '-'],      // Y-m equal
          'Ymd' => ['F j, Y', '', ''],      // Y-m-d equal
        ];
        foreach($formats as $key => $curFormat){
          if($key == "" OR $start->format($key) == $end->format($key)){
            $format = $curFormat;
          }
        }
      return implode($format[2], [$start->format($format[0]), $end->format($format[1])] );

答案 1 :(得分:0)

到目前为止,我想出了这个助手。我将其发布,以防万一有人发现它有用。

function date_range_string(\Carbon\Carbon $start_date, \Carbon\Carbon $end_date)
{
    if ($end_date->lessThan($start_date)) {
        throw new InvalidArgumentException('End date must be greater than start date');
    }

    $date_format = 'F jS, Y';

    $start_date_format = $date_format;
    $end_date_format = $date_format;

    if ($start_date->year == $end_date->year) {
        $start_date_format = 'F jS';

        if ($start_date->month == $end_date->month) {
            $end_date_format = 'jS, Y';

            if ($start_date->day == $end_date->day) {
                return $start_date->format($date_format);
            }
        }
    }

    return $start_date->format($start_date_format) . '-' . $end_date->format($end_date_format);
}

答案 2 :(得分:0)

您可以这样做,

        function date_period_format($period_start,$period_end){

            $period_start = date_create_from_format("Y-m-d",$period_start);
            $period_end = date_create_from_format("Y-m-d",$period_end);
            if($period_end->format("Y m") == $period_start->format("Y m")){
                return $period_start->format("F jS") . "-" . $period_end->format("jS, Y");
            }elseif($period_end->format("Y") == $period_start->format("Y")){
                return $period_start->format("F jS") . "-" . $period_end->format("F jS, Y");
            }else{
                return $period_start->format("F jS, Y") . " - " . $period_end->format("F jS, Y");
            }
        }
        echo date_period_format('2020-05-20', '2021-05-30');

答案 3 :(得分:0)

$start_date = '2019-12-25';
$end_date = '2020-01-03';


// expected output 'Desember 25th, 2019 - January 3rd, 2020'
$startDateTime = \DateTime::createFromFormat("Y-m-d", $start_date);
$endDateTime =  \DateTime::createFromFormat("Y-m-d", $end_date);
$result = ($startDateTime->format("F jS, Y"))." - ".($endDateTime->format("F jS, Y"));
echo $result;

$start_date = '2019-10-24';
$end_date = '2019-11-02';

// expected output 'October 24th-November 2nd, 2019'
$startDateTime = \DateTime::createFromFormat("Y-m-d", $start_date);
$endDateTime =  \DateTime::createFromFormat("Y-m-d", $end_date);
$result = ($startDateTime->format("F jS"))."-".($endDateTime->format("F jS, Y"));
echo $result;

$start_date = '2020-05-20';
$end_date = '2020-05-30';

// expected output 'May 20th-30th, 2020'
$startDateTime = \DateTime::createFromFormat("Y-m-d", $start_date);
$endDateTime =  \DateTime::createFromFormat("Y-m-d", $end_date);
$result = ($startDateTime->format("F jS"))."-".($endDateTime->format("jS, Y"));
echo $result;

答案 4 :(得分:0)

如果DateTime格式的变体存储在数组中,则可以很容易地交换它们,以其他语言为例。

function strDateRange($start, $end, $formats = null){
  $formats = $formats ?? [
    '' => ['F jS, Y','F jS, Y'],  //default
    'Y' => ['F jS','F jS, Y'],    //years equal
    'Ym' => ['F jS','jS, Y'],     //years and month equal
  ];
  foreach($formats as $key => $curFormat){
    if($key == "" OR $start->format($key) == $end->format($key)){
      $format = $curFormat;
    }
  }
return $start->format($format[0])."-".$end->format($format[1]);
}

测试:

echo strDateRange(date_create('2019-04-20'), date_create('2019-04-30'))."<br>\n";
echo strDateRange(date_create('2019-04-20'), date_create('2019-05-30'))."<br>\n";
echo strDateRange(date_create('2019-04-20'), date_create('2020-05-30'))."<br>\n";

输出:

April 20th-30th, 2019
April 20th-May 30th, 2019
April 20th, 2019-May 30th, 2020

注意:该函数还接受DateTime的所有扩展,例如Carbon和dt