Integrate carbon library in codeigniter 3
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.
2 Answers
2
Easy steps:
Direct download: https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php
Put Carbon.php at application/libraries
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
Put this in your Controller
$this->load->library ( 'Mcarbon' );
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);
Here is another method for Codeigniter 3:
Install using Composer (I don't explain here how install Composer):
composer require nesbot/carbon ^2
ˆ2
Update Codeigniter config file to autoload the component:
$config['composer_autoload'] = FALSE;
$config['composer_autoload'] = FCPATH.'/vendor/autoload.php';
Create a Codeigniter library for Carbon:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use CarbonCarbon;
public function __construct($time = null, $tz = null)
parent::__construct($time, $tz);
Load the library in your controller, model...:
$this->load->library('carbon_lib');
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.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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 7:04