dongyi6269 2016-02-06 19:26
浏览 66
已采纳

如何在Laravel中创建一个Carbon日期

How do you I create a Carbon date if I have the date part in the following format 'd M Y' and I have hours and minutes in separate variables as integers.

eg.

$this->start_dt  = '06 Feb 2016'
$this->start_hr  = '12'
$this->start_min  = '00'

Currently I'm having to do this but it also ends up adding the current seconds.

Carbon::createFromFormat('d M Y', $this->start_dt)->hour($this->start_hr)->minute($this->start_min);

result 2016-02-06 12:00:44

Is there a more cleaner way to create a carbon date without having to chain the hours and minutes and to set the seconds to 00?

Maybe something like this but it doesn't work:

Carbon::createFromFormat('d M Y H:i', $this->start_dt, $this->start_hr, $this->start_min);

* UPDATE *

Manage to figure out:

 Carbon::createFromFormat('d M Y H:i:s', $this->start_dt . ' ' . $this->start_hr . ':'. $this->start_min . ':00');
  • 写回答

2条回答 默认 最新

  • doumiao0498 2016-02-06 19:36
    关注

    Documentation: Instantiation

    Carbon::createFromDate($year, $month, $day, $tz);
    Carbon::createFromTime($hour, $minute, $second, $tz);
    Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
    

    Documentation: Fluent setters

    $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString();
    $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString();
    $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString();
    

    Solution:

    Carbon::createFromFormat('d M Y', $this->start_dt)
        ->setTime($this->start_hr, $this->start_min);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题