Skip to content
Advertisement

Integrate carbon library in codeigniter 3

i’m having hard time trying to integrate the grate DateTime Library carbon library with in my project in codeigniter 3 I tried this

$this->load->library('carbon');

and it’s give me an error

not existing class

i think the problem is namespaces because carbon uses namespace carboncarbon

Thank you in advance.

Advertisement

Answer

Easy steps:

  1. Direct download: https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php

  2. Put Carbon.php at application/libraries

  3. Create Mcarbon.php

    <?php
    
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    require_once dirname(__FILE__) . '/Carbon.php';
    
    use CarbonCarbon;
    
    class Mcarbon extends Carbon
    {}
    
  4. Put this in your Controller

    $this->load->library ( 'Mcarbon' ); 
    
  5. Call Carbon method in any function. Example:

    <?php
    
    $dt =Mcarbon::createFromDate(2018,2,13,null);
    var_dump($dt->year);
    var_dump($dt->month);
    var_dump($dt->day);
    var_dump($dt->hour);
    
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement