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

如何在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');

如果我的日期部分采用以下格式'd MY'和我,我如何创建碳日期? 在单独的变量中有小时和分钟作为整数。 p>

例如。 p>

  $ this-> start_dt = '06 2016年2月'
 $ this-> start_hr ='12'
 $ this-> start_min = '00'  
  code>  pre> 
 
 

目前我不得不这样做,但它最终还会添加当前秒数。 p>

  Carbon  :: createFromFormat('d M Y',$ this-> start_dt) - >小时($ this-> start_hr) - >分钟($ this-> start_min); 
  code>   pre> 
 
 

结果 2016-02-06 12:00:44 code> p>

是否有更简洁的方法来创建没有碳的日期 必须链接小时和分钟并将秒设置为00? p>

可能是这样的但它不起作用: p>

  Carbon :: createFromFormat('d MYH:i',$ this-> start_dt,$ this-> start_hr,$ this-> start_min); 
  code>  pre> 
 
   * UPDATE *  strong>  p> 
 
 

管理以确定: p>

  Carbon :: createFromFormat('d MYH  :i:s',$ this-> start_dt。''。$ this-> start_hr。':'。$ this-> start_min。':00'); 
  code>  pre>  
  div>

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);

If you are willing to use createFromFormat than you have to inout the second argument as a string like this :

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