Welcome Guest, Not a member yet? Register   Sign In
Autoloading Time Library
#1

(This post was last modified: 06-30-2020, 11:08 AM by php_rocs.)

Hello, I'm new to CI4 and didn't quite understand how can I autoload and use Time library.  
codeigniter.com/user_guide/libraries/time.html

I tried to load it via BaseController with using:
Code:
use CodeIgniter\I18n\Time;

$this->time = new Time();

But it didn't work. Can someone explain? I want to autoload it and use in the views.
Reply
#2

(This post was last modified: 06-30-2020, 11:36 AM by captain-sensible.)

<?php namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\I18n\Time;




$myTime = new Time('now');
$date= $myTime->toLocalizedString('MMM d, yyyy');
$data = [
'title' =>ucfirst($page) ,
'date'=>$date
];


-------------------

in view :

<?php echo $date ;?>



i use this in copywrite in footer:
<div class ="icon"><h4>&copy; 13/05/2020 to:<br> <?php echo $date ;?></h4> </div>
whatever day you view my web it will give something like:


Jun 30, 2020





your using a class member so yours would be :

$date= $this->time->toLocalizedString('MMM d, yyyy');
Reply
#3

Thank you for your answer but how is this autoloading while I have load it in every Controller? I'm looking for a way to load the library in BaseController and use its different functions in Views or other Controllers. (Just like in the CI3, using mdate($datestring, $time)) Shouldn't I add something to initController?


Code:
//--------------------------------------------------------------------
// Preload any models, libraries, etc, here.
//--------------------------------------------------------------------
// E.g.:
// $this->session = \Config\Services::session();
Reply
#4

(This post was last modified: 07-01-2020, 09:58 AM by captain-sensible.)

i think you can do this in baseController:




class BaseController extends \CodeIgniter\Controller
{
protected $helpers = [];

public function __construct()
{
helper(['date', 'form']);
}
}

in fact i just tried it on a test controller , which of course needs:

class YourController extends BaseController
{



doing that and then using in controller ,without using any helper in controller
echo now('Australia/Victoria'); // this produced 1593672836


which when adequately formated would be : Thursday, July 2, 2020 6:53:56am
Reply
#5

Thank you but i'm not trying to load helper, i'm trying to load CodeIgniter\I18n\Time library
Reply
#6

(This post was last modified: 07-01-2020, 11:57 AM by captain-sensible.)

to expand on the above i would be interested to see what others think :

if i have in BaseController :

class BaseController extends Controller
{

protected $helpers = [];
protected $theTime;
protected $theDate;


public function __construct()
{
helper(['text', 'date']);

$this->theTime = now('Australia/Victoria');
$this->theDate = date("d/m/Y",$this->theTime);
}











then in a controller which does :

class Spam extends BaseController
{

public function __construct() {
parent::__construct();
}



//i should now be able to access parent class member as follows:


echo $this->theDate;

in which case that could be passed to any view


$data = [

'date'=>$this->theDate
];


probably getter & setter methods might be better
Reply
#7

As I said in my earlier post this isn't what i'm looking for. I'm trying to autoload time library not the helper. Thanks for your help anyway.
Reply
#8

(This post was last modified: 07-03-2020, 12:56 PM by captain-sensible.)

ok well I saw a post on support thread about how to create your own library , when really it was about how to create your own class. Libraries to my memory used to be Java Jar files. But to some class and library are the same thing .

Autoload can also mean , you can make use of methods anywhere.

So this is what i've played with recently.

In BaseConroller :

use CodeIgniter\Controller;
use CodeIgniter\I18n\Time;
/* this line to me means I can now make use of system/I18n/Time.php but i haven't yet instantiated it*/



class BaseController extends Controller
{


but..and this is just one way that works , as you say also public intitConroller. Its just easier to keep code separate for me

in BaseConroller:


protected $helpers = [];
protected $theTime;
protected $theDate;
protected $mytime;



public function __construct()
{
helper(['text', 'date','uri','html','form','security','number']);

$this->theTime = now('Europe/London');
$this->myTime = new Time('now', 'America/Chicago', 'en_US');// this line makes use of Time class located system/I18n/Time.php
}




in a controller just doing this :
echo "get from base controller".$this->myTime;
//the above $this->myTime is the class member (some call it property) from the BaseController

The way i've done it will work, as long as you don't declare a _construct in any child class controller. If you use the public function initController {

it doesn't seem to matter.So in the above I used "use" in BaseConroller ; that means that any child class extending from BaseController will have access to protected class method and properties of the parent BaseController ( i would call that autoloading).

I can then instantiate a class member of BaseController called $mytime to give it a value , that can be accessed anywhere as $this->myTime.

Does this help ?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB