Php 如何添加一个新的碳函数来处理laravel中的碳日期?

Php 如何添加一个新的碳函数来处理laravel中的碳日期?,php,laravel,date,datetime,php-carbon,Php,Laravel,Date,Datetime,Php Carbon,我知道碳是如何工作的,但我想创建自定义碳功能。我有一个名为diffForHumans()的函数,我想对该函数做一些修改,并调用该函数diffForHumansCustom()。如果我编辑Carbon.phpvendor文件,我可以实现我的目标,但是修改将在composer更新之后进行。任何建议或代码帮助都将不胜感激 原函数 我的修改函数 公共函数diffForHumansCustom(碳$other=null,$absolute=false) { $isNow=$other==null; 如果(

我知道碳是如何工作的,但我想创建自定义碳功能。我有一个名为
diffForHumans()
的函数,我想对该函数做一些修改,并调用该函数
diffForHumansCustom()
。如果我编辑
Carbon.php
vendor文件,我可以实现我的目标,但是修改将在
composer更新之后进行。任何建议或代码帮助都将不胜感激

原函数 我的修改函数
公共函数diffForHumansCustom(碳$other=null,$absolute=false)
{
$isNow=$other==null;
如果($isNow){
$other=static::now($this->tz);
}
$diffInterval=$this->diff($other);
开关(真){
案例($diffInterval->y>0):
$单位='年';
$delta=$diffInterval->y;
打破
案例($diffInterval->m>0):
$单位='月';
$delta=$diffInterval->m;
打破
案例($diffInterval->d>0):
$单位='天';
$delta=$diffInterval->d;
如果($delta>=self::DAYS\u/周){
$单位='周';
$delta=地板($delta/self:$DAYS\u/周);
}
打破
案例($diffInterval->h>0):
$单位='小时';
$delta=$diffInterval->h;
打破
案例($diffInterval->i>0):
$单位='分钟';
$delta=$diffInterval->i;
打破
违约:
$delta=$diffInterval->s;
$unit='second';
打破
}
如果($delta==0){
$delta=1;
}
$txt=$delta.'.$unit;
$txt.=$delta==1?'''s';

如果($unit=='second'&&$delta,您可以扩展Carbon类,然后使用子类代替Carbon,或者使用
diffForHumansCustom()
方法创建一个Trait,并在类中使用它

扩展碳\碳:

<?php

    namespace App\Http\Controllers\Helpers;

    use Carbon\Carbon;

    class CarbonCopy extends Carbon {

        public function diffForHumansCustom(Carbon $other = null, $absolute = false) {
            // YOUR UNIQUE IMPLEMENTATION, CODE & LOGIC...
        }

    }
<?php

    namespace App\Http\Controllers\Helpers;

    use Carbon\Carbon;

    trait CarbonT {     

        public function diffForHumansCustom(Carbon $other = null, $absolute = false) {
            // YOUR UNIQUE IMPLEMENTATION, CODE & LOGIC...
        }

    }

我接受了@Poiz的回答,但我只想告诉你们我是如何做到的

创建了一个模型
IddCarbon.php
使用
对于2019年来到这里的任何人,我发现一个更简洁的解决方案是使用Laravel
ServiceProvider

1) 使用
php artisan make:provider-CarbonServiceProvider创建您的服务提供商

服务提供商

<?php

namespace App\Providers;

use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

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));
        });
    }
}
<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ExampleController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        dd(Carbon::easterDate(2015));
    }
}
3) 现在,您可以从应用程序中的任何位置访问宏

示例控制器

<?php

namespace App\Providers;

use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

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));
        });
    }
}
<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ExampleController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        dd(Carbon::easterDate(2015));
    }
}

享受吧!

创建您自己的类,扩展
Carbon
(例如
MyCarbon
),并在您的类版本中实现该方法
<?php
namespace App\Models;

use Carbon\Carbon 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 = App\Models\IddCarbon::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;
<?php

namespace App\Providers;

use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

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));
        });
    }
}
<?php

return [

    'providers' => [

        ...

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\DatabaseServiceProvider::class,
        App\Providers\CarbonServiceProvider::class,

        ...
    ],
];
<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ExampleController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        dd(Carbon::easterDate(2015));
    }
}
Carbon @1428192000 {#2663 ▼
  date: 2015-04-05 00:00:00.0 UTC (+00:00)
}