【问题标题】:Laravel carbon get year month format form two dateLaravel 碳从两个日期获取年月格式
【发布时间】:2020-02-07 17:46:57
【问题描述】:

我在 MySQL DATE 列类型的数据库中存储了 from_date 和 to_date。 如何以格式获取 from_date 和 to_date 之间的日期差异 ..eg 1 Year 2 Months

2018-01-05
2019-02-22

2 Year 1 Month

【问题讨论】:

标签: php laravel


【解决方案1】:

试试这个。

  $datetime1 = new DateTime("2018-01-05");
  $datetime2 = new DateTime("2019-02-22");
  $difference = $datetime1->diff($datetime2);
  echo !empty($difference->y) ? $difference->y.' Year ':'';
  echo !empty($difference->m) ? $difference->m.' Month ':'';

输出将是

   1 Year 1 Month

以碳的方式

 use Carbon\Carbon;

 $startDate = Carbon::parse('2018-01-05'); 
 $endDate = Carbon::parse('2019-02-22'); 
 $diff = $startDate->diffInYears($endDate);
 $diffs = $startDate->diffInMonths($endDate);

 echo $diff;
 echo $diffs;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    相关资源
    最近更新 更多