0

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 carbon\carbon

Thank you in advance.

Community
  • 1
  • 1
Ahmed Nagi
  • 97
  • 11
  • I guess if you still didn't find an answer for this problem, I think it's better for you to have a look at my answer bellow! :-) – Randika Vishman Jan 21 '17 at 07:04

3 Answers3

4

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 Carbon\Carbon;
    
    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);
    
Pang
  • 9,564
  • 146
  • 81
  • 122
Jetwiz
  • 642
  • 7
  • 14
2

Here is another method for Codeigniter 3:

  1. Install using Composer (I don't explain here how install Composer):

    • Open the terminal
    • Go to the root folder of your Codeigniter project and type:
      composer require nesbot/carbon ^2
    • It should install Carbon 2.x in the vendor/nestbot/Carbon folder
    • Remove the ˆ2 to install the latest 1.x version (when writing this answer)
  2. Update Codeigniter config file to autoload the component:

    • Open application/config/config.php and change:
      $config['composer_autoload'] = FALSE;
      to:
      $config['composer_autoload'] = FCPATH.'/vendor/autoload.php';
  3. Create a Codeigniter library for Carbon:

    • Create the application/libraries/Carbon_lib.php file:
    <?php
    
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    use Carbon\Carbon;
    public function __construct($time = null, $tz = null) {
      parent::__construct($time, $tz);
    }
  4. Load the library in your controller, model...:

    $this->load->library('carbon_lib');
  5. Use the library as you would with Carbon:

    $carbon = $this->carbon_lib;
    $carbon->addDay();
    //Get the SQL date and date/time format
    $carbon->toDateString();
    $carbon->toDateTimeString();
    //And to create an immutable object
    $carbonImmutable = $this->carbon_lib->toImmutable(); ...

See Carbon website for more details.

parisiam
  • 31
  • 3
1

Follow the steps for Codeigniter 3:

  1. Install carbon via composer

    composer require nesbot/carbon

  2. Update index.php, Add this line

    require 'vendor/autoload.php';

  3. In your controller add this line at the top.

    use Carbon\Carbon;

Now you can use carbon functionalities. example:

Carbon::now();
Eishfaq Ahmed
  • 129
  • 1
  • 6