【问题标题】:How to add CarbonInterval instance in Carbon instance如何在 Carbon 实例中添加 CarbonInterval 实例
【发布时间】:2018-06-12 00:57:09
【问题描述】:

我有一个碳实例

   $a = Carbon\Carbon::now();

   Carbon\Carbon {
     "date": "2018-06-11 10:00:00",
     "timezone_type": 3,
     "timezone": "Europe/Vienna",
   }

还有一个 CarbonInterval 实例

   $b = CarbonInterval::make('1month');


     Carbon\CarbonInterval {
     "y": 0,
     "m": 1,
     "d": 0,
     "h": 0,
     "i": 0,
     "s": 0,
     "f": 0.0,
     "weekday": 0,
     "weekday_behavior": 0,
     "first_last_day_of": 0,
     "invert": 0,
     "days": false,
     "special_type": 0,
     "special_amount": 0,
     "have_weekday_relative": 0,
     "have_special_relative": 0,
   }

如何在碳实例中添加间隔以便我得到

   Carbon\Carbon {
     "date": "2018-07-11 10:00:00",
     "timezone_type": 3,
     "timezone": "Europe/Vienna",
   }

我知道涉及将其转换为像这样的时间戳或 Datetime 类的解决方案

strtotime( date('Y-m-d H:i:s', strtotime("+1 month", $a->timestamp ) ) );  

这是我目前正在使用的,但我正在寻找一种更“碳”的方式,我通过 official site 搜索但找不到任何关于此的内容,因此需要一些帮助。

更新: 只是为了给你上下文 在前端我有两个控件第一个是间隔(天,月,年)第二个是一个文本框,所以根据组合我动态生成字符串,如 "2 days" , "3 months" 所以然后将其提供给间隔类

【问题讨论】:

    标签: php laravel php-carbon


    【解决方案1】:

    我不知道添加间隔的内置函数,但应该将间隔的总秒数添加到日期:

    $date = Carbon::now(); // 2018-06-11 17:54:34
    $interval = CarbonInterval::make('1hour');
    
    $laterThisDay = $date->addSeconds($interval->totalSeconds); // 2018-06-11 18:54:34
    

    编辑:找到更简单的方法!

    $date = Carbon::now(); // 2018-06-11 17:54:34
    $interval = CarbonInterval::make('1hour');
    
    $laterThisDay = $date->add($interval); // 2018-06-11 18:54:34
    

    这是因为Carbon 基于DateTimeCarbonInterval 基于DateInterval。方法参考见here

    【讨论】:

      【解决方案2】:

      参见文档https://carbon.nesbot.com/docs/#api-addsub

      $carbon = Carbon\Carbon::now();
      $monthLater = clone $carbon;
      $monthLater->addMonth(1);
      dd($carbon, $monthLater);
      

      结果是

      Carbon {#416 ▼
        +"date": "2018-06-11 16:00:48.127648"
        +"timezone_type": 3
        +"timezone": "UTC"
      }
      
      Carbon {#418 ▼
        +"date": "2018-07-11 16:00:48.127648"
        +"timezone_type": 3
        +"timezone": "UTC"
      }
      

      对于这个间隔[月、世纪、年、季度、日、工作日、周、小时、分钟、秒],您可以使用输入

      $count = 1; // for example
      $intrvalType = 'months'; // for example
      $addInterval = 'add' . ucfirst($intrvalType);
      $subInterval = 'sub' . ucfirst($intrvalType);
      $carbon = Carbon\Carbon::now();
      dd($carbon->{$addInterval}($count));
      dd($carbon->{$subInterval}($count));
      

      【讨论】:

      • 谢谢,但我正在动态生成间隔。如果我这样做,我将需要大量 if else 来处理每种情况(天、月等),这是我首先要避免的。
      • 你能把你的动态部分代码转换成字符串吗??
      • 如果可以的话,你可以使用这个代码$carbon->addSeconds(1);
      • CarbonInterval::make('{$count}{$interval}'); 这里这两个变量来自用户,这取决于它生成适当的间隔,例如像$count=2$interval = 'days' 这样的字符串给出2days;或另一个示例 $count=3$interval = 'months' 给出“3months”,然后您将其提供给 CarbonInterval 以获得 3 个月的间隔
      • @Viney 查看我的更新答案,如何使用我建议的方法进行操作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      相关资源
      最近更新 更多