I'm trying to use Carbon API(https://github.com/briannesbitt/Carbon) for my project in Codeigniter. But I am having trouble using it.
I first installed it with composer. But when I use the following code in my project:
require 'vendor/autoload.php';
use Carbon\Carbon;
printf("Now: %s", Carbon::now());
I get the following error:
syntax error, unexpected 'use' (T_USE) in ...
So, I tried installing it manually(Copy and past the Carbon class file to my project). Then it worked fine when I tested the following code:
$now = Carbon::now();
die($now);
But when I try to run something like this:
$dtKtm = Carbon::createFromFormat('Y-m-d H:i:s', $data['banners'][0]['date_added']);
echo Carbon::now()->diffForHumans($dtKtm, false);
It gives me " Class 'Translator' not found" error.
Can anyone help me fix this.
Carbon Date Time library usage with CodeIgniter
CodeIgniter App setup:
First let me give you an introduction about my CodeIgniter project setup on the localhost is as follows:
/
|- gheapp
| |- application
| |- system
| L- vendor
| | |-bin
| | |-composer
| | |-nesbot/carbon
| | |-symfony
| |
| |----- composer.json
| L----- composer.lock
|
|- public_html
| |- .htaccess
| L- index.php
And in the index.php
I have set up the following paths to the system and the application:
$system_path = '../gheapp/system';
$application_folder = '../gheapp/application';
Note 1: by doing so, our application source code becomes hidden to the public at first.
Note 2: For more nitty-gritty about my CodeIgniter setup please follow the following StackOverflow Answer of mine
Step 1: how do I fetch the Carbon package.
Using Composer I fetched the Cabron
package to my CodeIgniter App. My composer.json
looks like following (it's simplified for your ease).
{
"require": {
"nesbot/carbon": "^1.22"
}
}
After you run composer command composer install
it creates the vendor
folder within your CI app root and puts the Carbon package as shown above in the folder structure.
Step 2: Autoload Composer downloaded Packages:
In my index.php
before the line at the bottom
require_once BASEPATH.'core/CodeIgniter.php';
I have put the following two lines:
$composer_vendor_path = '../gheapp/vendor';
require_once $composer_vendor_path.'/autoload.php';
So now my CodeIgniter
app knows how and where to load the composer installed Packages.
Step 3: how do I use the Carbon package within my controller:
Within any method function of my controller (or model) I can use Carbon
as follows:
$date = Carbon\Carbon::today();
echo $date; // output: 2017-01-21 00:00:00
echo '<br/>'.$date->diffForHumans(); // output: 7 hours ago
For further details about using Carbon please visit the following link: Carbon from nesBot
I hope my answer will be useful/helpful to somebody else out there in the future! :-)
You need to autoload the vendor folder in your bootstrap (In the index.php
file before the line at the bottom require_once BASEPATH.'core/CodeIgniter.php';
).
require __DIR__ . '/vendor/autoload.php';
The you need to import the libraries namespace in your model / controller (wherever you are using Carbon).
use Carbon\Carbon;
Only then can you use the library.
the easiest way that i found is:
install carbon using:
composer require nesbot/carbon
open application/config/config.php and change:
$config['composer_autoload'] = FALSE;
or whatever
to
$config['composer_autoload'] = FCPATH . '/vendor/autoload.php';
call whatever carbon function in your controller:
$data['carbondate'] = Carbon\Carbon::now();