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.





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




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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)