How to get current date time in codeigniter?

How to get current date time in Codeigniter– It is pretty simple & quick to get the current time & date in Codeigniter. First, load date helper in the controller or autoload it in the _autoload file, After loading this helper you will be able to access its methods defined for time & date.

Get current date time with Date Format in Codeigniter.

How to get current date time in codeigniter

Now let us go step by step to understand how to date helper is used. 

Load Date Helper

First Make sure the date helper is loaded, If not then load it simply as below-

$this->load->helper('date');

Get Date Time

Once the date helper is loaded you get the current date and time as –

$format = "%Y-%m-%d %h:%i %A";
echo mdate($format);
// this will print 
// 2017-09-06 08:15 PM 

Date Format

You can pass the date time format to the date($format) function where the $format is the date format. Apart from Codeigniter DateTime, we can also use native date functions to get the current DateTime – For example, you can use the now() function to get the current time & date.

Codeigniter Date Time Demo

<!--?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mydate extends CI_Controller {
function getCurrentDate(){

$this--->load->helper('date');

$format = "%Y-%m-%d %h:%i %A";
echo mdate($format);

}

}
?>

Try it »

If run the above example it will produce output something like this- How to get current date time in codeigniter

For More Details About Date Helper & Functions Read – Date helper

I hope this article was helpful for you.


Advertisements

Add Comment

📖 Read More