我知道碳是如何工作的,但我想创建自定义碳函数。碳原子有一个叫做diffForHumans()
的函数,我想对这个函数做一些修改,叫它diffForHumansCustom()
。如果我编辑Carbon.php
供应商文件,我可以实现我的目标,但修改将在composer update
之后消失。任何建议或代码帮助是感激的。
public function diffForHumans(Carbon $other = null, $absolute = false)
{
$isNow = $other === null;
if ($isNow) {
$other = static::now($this->tz);
}
$diffInterval = $this->diff($other);
switch (true) {
case ($diffInterval->y > 0):
$unit = 'year';
$delta = $diffInterval->y;
break;
case ($diffInterval->m > 0):
$unit = 'month';
$delta = $diffInterval->m;
break;
case ($diffInterval->d > 0):
$unit = 'day';
$delta = $diffInterval->d;
if ($delta >= self::DAYS_PER_WEEK) {
$unit = 'week';
$delta = floor($delta / self::DAYS_PER_WEEK);
}
break;
case ($diffInterval->h > 0):
$unit = 'hour';
$delta = $diffInterval->h;
break;
case ($diffInterval->i > 0):
$unit = 'minute';
$delta = $diffInterval->i;
break;
default:
$delta = $diffInterval->s;
$unit = 'second';
break;
}
if ($delta == 0) {
$delta = 1;
}
$txt = $delta . ' ' . $unit;
$txt .= $delta == 1 ? '' : 's';
if ($absolute) {
return $txt;
}
$isFuture = $diffInterval->invert === 1;
if ($isNow) {
if ($isFuture) {
return $txt . ' from now';
}
return $txt . ' ago';
}
if ($isFuture) {
return $txt . ' after';
}
return $txt . ' before';
}
修改后的函数
public function diffForHumansCustom(Carbon $other = null, $absolute = false)
{
$isNow = $other === null;
if ($isNow) {
$other = static::now($this->tz);
}
$diffInterval = $this->diff($other);
switch (true) {
case ($diffInterval->y > 0):
$unit = 'year';
$delta = $diffInterval->y;
break;
case ($diffInterval->m > 0):
$unit = 'month';
$delta = $diffInterval->m;
break;
case ($diffInterval->d > 0):
$unit = 'day';
$delta = $diffInterval->d;
if ($delta >= self::DAYS_PER_WEEK) {
$unit = 'week';
$delta = floor($delta / self::DAYS_PER_WEEK);
}
break;
case ($diffInterval->h > 0):
$unit = 'hour';
$delta = $diffInterval->h;
break;
case ($diffInterval->i > 0):
$unit = 'minute';
$delta = $diffInterval->i;
break;
default:
$delta = $diffInterval->s;
$unit = 'second';
break;
}
if ($delta == 0) {
$delta = 1;
}
$txt = $delta . ' ' . $unit;
$txt .= $delta == 1 ? '' : 's';
if($unit == 'second' && $delta<=59) {
return 'Just now';
}
// Greater than 3 days
// [xx] [Month] [year, only displays if not current year'] at [time in 24 clock].
if(($unit == 'day' && $delta > 3) || ($unit == 'week') || ($unit == 'month') || ($unit == 'year')) {
$timestamp = $this->getTimestamp();
$curYear = date('Y');
$y = ($curYear == date('Y', $timestamp)) ? '': date('Y', $timestamp);
$date = date('j F '.$y, $timestamp);
$time = date('H:i', $timestamp);
$txt = rtrim($date).' at '.$time;
return $txt;
}
if ($absolute) {
return $txt;
}
$isFuture = $diffInterval->invert === 1;
if ($isNow) {
if ($isFuture) {
return $txt . ' from now';
}
return $txt . ' ago';
}
if ($isFuture) {
return $txt . ' after';
}
return $txt . ' before';
}
对于2019年来到这里的任何人,我发现添加Carbon::macro
函数的更整洁的解决方案是使用Laravel ServiceProvider
1)使用php artisan make:provider CarbonServiceProvider
创建服务提供商
CarbonServiceProvider
<?php
namespace AppProviders;
use CarbonCarbon;
use IlluminateSupportServiceProvider;
class CarbonServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register()
{
}
/**
* Bootstrap services.
*/
public function boot()
{
Carbon::macro('easterDate', function ($year) {
return Carbon::createMidnightDate($year, 3, 21)->addDays(easter_days($year));
});
}
}
2)在config/app.php
app/config
<?php
return [
'providers' => [
...
/*
* Application Service Providers...
*/
AppProvidersAppServiceProvider::class,
AppProvidersAuthServiceProvider::class,
AppProvidersBroadcastServiceProvider::class,
AppProvidersEventServiceProvider::class,
AppProvidersRouteServiceProvider::class,
AppProvidersDatabaseServiceProvider::class,
AppProvidersCarbonServiceProvider::class,
...
],
];
3)你现在可以在你的应用程序的任何地方访问你的宏。
ExampleController
<?php
namespace AppHttpControllers;
use CarbonCarbon;
use IlluminateHttpRequest;
use AppHttpControllersController;
class ExampleController extends Controller
{
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function index(Request $request)
{
dd(Carbon::easterDate(2015));
}
}
在这个例子中,这应该返回
Carbon @1428192000 {#2663 ▼
date: 2015-04-05 00:00:00.0 UTC (+00:00)
}
享受吧!
你可以扩展碳类,然后使用你的子类来代替碳,或者简单地用diffForHumansCustom()
方法创建一个Trait,并在你的类中使用它
扩展碳:
<?php
namespace AppHttpControllersHelpers;
use CarbonCarbon;
class CarbonCopy extends Carbon {
public function diffForHumansCustom(Carbon $other = null, $absolute = false) {
// YOUR UNIQUE IMPLEMENTATION, CODE & LOGIC...
}
}
使用Trait代替
<?php
namespace AppHttpControllersHelpers;
use CarbonCarbon;
trait CarbonT {
public function diffForHumansCustom(Carbon $other = null, $absolute = false) {
// YOUR UNIQUE IMPLEMENTATION, CODE & LOGIC...
}
}
Usage in Your Controller: as Trait
<?php
namespace AppHttpControllers;
use AppHttpControllersHelpersCarbonT;
use CarbonCarbon;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppUser;
class TestController extends Controller {
use CarbonT;
public function carbonRelatedOperation(){
$cbnDate = Carbon::createFromFormat("Y-m-d H:i:s", "2016-05-02 11:11:11");
$customDiff = $this->diffForHumansCustom($cbnDate);
dd( $customDiff );
}
}
在你的控制器内部使用:作为碳的子类
<?php
namespace AppHttpControllers;
use AppHttpControllersHelpersCarbonCopy;
use CarbonCarbon;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppUser;
class TestController extends Controller {
public function carbonRelatedOperation(){
$cbnDate = Carbon::createFromFormat("Y-m-d H:i:s", "2016-05-02 11:11:11");
$cbCopy = new CarbonCopy();
dd( $cbCopy->diffForHumansCustom($cbnDate) );
}
}
我已经接受了@Poiz的回答,但我只是想向你们展示我是如何做到的。
创建了一个模型IddCarbon.php
<?php
namespace AppModels;
use CarbonCarbon as Carbon;
class IddCarbon extends Carbon {
public function diffForHumansIdd(Carbon $other = null, $absolute = false)
{
$isNow = $other === null;
if ($isNow) {
$other = Carbon::now($this->tz);
}
$diffInterval = $this->diff($other);
switch (true) {
case ($diffInterval->y > 0):
$unit = 'year';
$delta = $diffInterval->y;
break;
case ($diffInterval->m > 0):
$unit = 'month';
$delta = $diffInterval->m;
break;
case ($diffInterval->d > 0):
$unit = 'day';
$delta = $diffInterval->d;
if ($delta >= Carbon::DAYS_PER_WEEK) {
$unit = 'week';
$delta = floor($delta / Carbon::DAYS_PER_WEEK);
}
break;
case ($diffInterval->h > 0):
$unit = 'hour';
$delta = $diffInterval->h;
break;
case ($diffInterval->i > 0):
$unit = 'minute';
$delta = $diffInterval->i;
break;
default:
$delta = $diffInterval->s;
$unit = 'second';
break;
}
if ($delta == 0) {
$delta = 1;
}
$txt = $delta . ' ' . $unit;
$txt .= $delta == 1 ? '' : 's';
if($unit == 'second' && $delta<=59) {
return 'Just now';
}
// Greater than 3 days
// [xx] [Month] [year, only displays if not current year'] at [time in 24 clock].
if(($unit == 'day' && $delta > 3) || ($unit == 'week') || ($unit == 'month') || ($unit == 'year')) {
$timestamp = $this->getTimestamp();
$curYear = date('Y');
$y = ($curYear == date('Y', $timestamp)) ? '': date('Y', $timestamp);
$date = date('j F '.$y, $timestamp);
$time = date('H:i', $timestamp);
$txt = rtrim($date).' at '.$time;
return $txt;
}
if ($absolute) {
return $txt;
}
$isFuture = $diffInterval->invert === 1;
if ($isNow) {
if ($isFuture) {
return $txt . ' from now';
}
return $txt . ' ago';
}
if ($isFuture) {
return $txt . ' after';
}
return $txt . ' before';
}
}
创建了一个辅助函数
function diffForHumans($date)
{
$timeDiff = AppModelsIddCarbon::parse($date);
return $timeDiff->diffForHumansIdd();
}
使用$article = Article::where('id','=','123')->first();//Eloquent result
$created = $article->created_at->toDateTimeString();//sample $created '2016-08-08 11:50:38'
$result = diffForHumans($created);
echo $result;