[英]Calculate difference between two dates using Carbon and Blade
Does anyone know how to pass a given variable instead the Carbon's default parameters?有谁知道如何传递给定变量而不是 Carbon 的默认参数?
The documentation of Carbon says: Carbon的文档说:
// CARBON SAMPLE
$dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto');
$dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver');
echo $dtVancouver->diffInHours($dtToronto); // 3
And i want to do something like this in my controller:我想在我的 controller 中做这样的事情:
// EXAMPLE
$date = "2016-09-16 11:00:00";
$datework = Carbon::createFromDate($date);
$now = Carbon::now();
$testdate = $datework->diffInDays($now);
And retrieving that on a Blade template并在 Blade 模板上检索它
// VIEW ON BLADE
<td> {{ $testdate }} </td>
You are not following the example from the Carbon Documentation .您没有遵循Carbon 文档中的示例。 The method Carbon::createFromDate()
expects 4 parameters: year , month , day and timezone . Carbon::createFromDate()
需要 4 个参数: year 、 month 、 day和timezone 。 And you are trying to pass a formatted date string.并且您正在尝试传递格式化的日期字符串。
If you want to create a Carbon object from a formatted date string you can use the constructor of the class just like this:如果你想从格式化的日期字符串创建一个 Carbon 对象,你可以像这样使用类的构造函数:
$date = "2016-09-17 11:00:00";
$datework = new Carbon($date);
Or you can use the static Carbon::parse()
method:或者您可以使用静态Carbon::parse()
方法:
$date = "2016-09-17 11:00:00";
$datework = Carbon::parse($date);
For your purposes you can use the this full example:出于您的目的,您可以使用以下完整示例:
$date = Carbon::parse('2016-09-17 11:00:00');
$now = Carbon::now();
$diff = $date->diffInDays($now);
And then in your Blade template:然后在你的 Blade 模板中:
<td> {{ $diff }} </td>
Blade Template刀片模板
A shorter code更短的代码
{{ $diff = Carbon\Carbon::parse($data->last_updated)->diffForHumans() }}
Result : 6 minutes ago结果 : 6 分钟前
您可以通过执行以下操作来清理代码并删除注释掉的代码:
<td>{{ $diff = Carbon\Carbon::parse($work['date'])->diffForHumans(Carbon\Carbon::now()) }} </td>
Carbon means you do not need to mix PHP Datetime and Carbon. Carbon 意味着你不需要混合 PHP Datetime 和 Carbon。 Once you have the datetime as a Carbon, simply do this...将日期时间作为 Carbon 后,只需执行此操作...
$comparisonTimeAsCarbon->diffAsCarbonInterval($theOtherTimeAsCarbon)
You can change diffAsCarbonInterval
to diffAsSeconds
, diffAsMinutes
and many more.您可以将diffAsCarbonInterval
更改为diffAsSeconds
、 diffAsMinutes
等等。
diffForHumans
is one of my faves. diffForHumans
是我的diffForHumans
之一。
Or, choose your own format with...或者,选择您自己的格式...
$comparisonTimeAsCarbon->diff($theOtherTimeAsCarbon)->format('%I:%S')
Carbon will even let you add text instead of a Carbon time, but, I recommend you use Carbon before you parse it, just in case. Carbon 甚至可以让您添加文本而不是 Carbon 时间,但是,我建议您在解析之前使用 Carbon,以防万一。
Shortest way最短路径
We can directly write it in blade我们可以直接写在blade中
<span>{{ \Carbon\Carbon::parse( $start_date )->diffInDays( $end_date ) }}</span>
You can use on a Blade template directly:您可以直接在 Blade 模板上使用:
{{ (Carbon\Carbon::parse(Auth::user()->created_at))->diffInDays(Carbon\Carbon::now()) }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.