×

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example:

$this->load->helper(‘date’);

Functions of Date Helper

  1. now(): The now () function takes the time from the local server, or the php supported timezone set in the config file that shows the current time in the form of Unix timestamp.

Syntax

echo now();

Parameters:  

$timezone:  It defines the time zone that is predefined in the config file, or it can be select from the local server.

UNIX timestamp: It shows the time in UNIX timestamp.

Note: If you have not defined a timezone, it shows the time according to the time_reference setting in config.

Example: Create a Tests.php file in application/controller folder and write the following program:

Tests.php

<?php
 class Tests extends CI_Controller
  {
     public function getCurrentDate() // define getCurrentDate() function that call on the URL
     {  
        $this->load->helper('date');  // loading of date helper 
         echo "<title> Tutorial And Example </title>"; 
       echo "<h2> USE of now() function </h2><br/>";
         echo now();  // define the now() function that call local’ server time
 }
 }
 ?> 

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function. It shows the output, as shown below.

CodeIgniter Date helper

2. mDate(): It represents the data in MySQL style date format. Where each letter uses % sign as a prefix. For example: %d %m %y.

Syntax

mdate($datestring, $time);

The method contains two parameters.

Parameters

  • $datestring: It defines the pattern of date, which you want to display in your system screen.
  • $time (int): It defines the time in UNIX timestamp.

Note: If you have not provided timestamp in second parameters, by default, it selects the current time of your system.

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
     public function getCurrentDate() // define getCurrentDate() function that call on the URL
     {  
        $this->load->helper('date');  // loading of date helper
         echo "<title> Tutorial And Example </title>"; 
            echo "<h3> Use of mdate() function  </h3>";
        $dates = ' Day: %d Month: %m Year: %Y  - %h:%i:%s %a'; 
        $time = time();
         echo mdate($dates, $time);
 }
 }
 ?> 

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function. It shows the output, as shown below.

CodeIgniter Date helper

3. standard_date(): It is used to display a standard format of date and time in the system.

Syntax

standard_date( [$format = ‘ DATE_1036’ [, $time =
NULL] ]);

Parameters

  • $format (string): It denotes the date format that you want to use.
  • $time – It shows the time in the UNIX timestamp.

There are some standard formats of date helpers, as shown below:

Constant Output
DATE_COOKIE Tue, 21 Jan 2020 13:06:55 UTC
DATE_ATOM 2020-01-21T 13:07:20+0000
DATE_RFC822 Tue, 21Jan 20 13:08:20 UTC
DATE_RFS850 Tuesday, 21-Jan-20 13:10:20 UTC
DATE_RSS Tue, 21 Jan 2020 13:10:50 UTC
DATE_RFC1036 Tuesday, 21-Jan-20 13:11:10 UTC
DATE_RFC1123 Tue, 21 Jan 2020 13:11:40 UTC
DATE_RFC2822 Tue, 21 Jan 2020 13:11:55 +0000
DATE_W3C 2020-01-21T 13:12:05+0000
DATE_ISO8601 2020-01-21T 13:13:03+00:00

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
     public function getCurrentDate() 
     {  
        $this->load->helper('date');  // loading the date helper
         echo "<title> Tutorial And Example </title>"; 
            // standard_date() function
         echo "<h3> USE of standard_date() function </h3> "; 
       $format= 'DATE_RFC850'; 
      $time = time();
     echo standard_date($format, $time);
 }
 }
 ?> 

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function. It shows the output, as shown below.

CodeIgniter Date helper

4. local_to_gmt() function: This function is used to convert the UNIX timestamp into GMT time.

Syntax

local_to_gmt( $time())

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  // loading the date helper
         echo "<title> Tutorial And Example </title>"; 
        echo “ Convert time from Local to gmt time <br/>”; 
 $gmt = local_to_gmt(time());
 echo ($gmt);
 }  
 } 
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function, it shows the output, as shown below.

CodeIgniter Date helper

5. gmt_to_local() function: It takes the UNIX timestamp (in GMT) as input and converts into localized timestamp that set on the timezone and Daylight saving time.

Syntax

gmt_to_local($timestamp,
$timezone, $daylight_saving);

Parameters:

  • $timestamp (int): It is used to take UNIX timestamp as an input.
  • $timezone: It defines the timezone. For example, UM12, UM11, UM9, UM8, UM7, etc.
  • $daylight_saving or $dst (bool): It takes Boolean value.

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  // loading the date helper
         echo "<title> Tutorial And Example </title>"; 
        echo "Convert time from gmt to local time <br>"; 
 $timestamp = 1579564573;
 $timezone  = 'UP55';
 $daylight_saving = TRUE;
 echo gmt_to_local($timestamp, $timezone, $daylight_saving);
 }  
 } 
 ?>     

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate function. It shows the output, as shown below.

CodeIgniter Date helper

6. mysql_to_unix(): It takes MySQL timestamps as input and return the time in UNIX timestamp.

Syntax

mysql_to_unix(  $time = ‘’]);

Here, $time (string) is used as input of MySQL timestamp.

Example: Now create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
        echo "Convert time from MySQL to UNIX timestamp <br>"; 
 $data = mysql_to_unix('20200121050935'); // it is in the format of yyyy-mm-dd-hh-mm-ss
 echo $data;
 }  
 } 
 ?>       

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function. It shows the output, as shown below.

CodeIgniter Date helper

7. unix_to_human() function: It takes Unix timestamps as input and returns the date and time in a human-readable format like yyyy-mm-dd hh:mm:ss am/pm.

Syntax

unix_to_human( $time = ‘ ’ [, $seconds =FALSE [, $fmt
= ’in’] ] ]);

Parameters

  • $time (int) – It defines the Unix timestamps.
  • $seconds (bool) – It defines whether you want to show seconds using Boolean TRUE or FALSE respectively.
  • $fmt = It defines the format of date as per the country name. For example, in, us, aus etc.

Example: Now create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 echo "Convert time from Unix  to human readable format YYYY-MM-DD HH:MM:SS AM/PM <br>";
 $now = time();
 echo unix_to_human($now); // INDIA time withoutseconds
 echo " Time without Second";
 echo "</br>";
 echo unix_to_human($now, TRUE, 'in'); // INDIA time with seconds
 echo "  Time with Second";
 }  
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function. It shows the output, as shown below.

CodeIgniter Date helper

8. human_to_unix() Function: It is opposite to the unix_to_human() function. It takes human readable time as input and return the time in Unix timestamp.

Syntax

$unixForm = human_to_unix($humanReadable);

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 $now = time();
 $humanReadable = unix_to_human($now);
 echo "Human Readable Format of Date :YYYY-MM-DD HH:MM AM/PM: ";
 echo ($humanReadable); // it will show date in human redable format
 echo "</br>";
 $unixForm = human_to_unix($humanReadable); // it will call human readable date //and convert into Unix timestamp.
 echo "In Unix timestamp:";
 echo ($unixForm);}  
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function, it shows the output, as shown below.

CodeIgniter Date helper

9. nice_date() Function: As the name suggests, it produces a nice format of date by improving the poor or unordered format of date.

Syntax

nice_date( [$bad_date =’ ‘  [, $format = False ] ]);

It has two parameters $bad_date and $format. The $bad_date parameter defines the bad format of date as bad. Whereas, $format uses true or false values such as Boolean values to return the date format as bad_date or not.

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 $bad_date1 = '20200122';
 $bad_date2 = '1-22-2020'; 
 $bad_date3 = '202001';
 echo "This showing the bad format of date:"." date1"." ".$bad_date1."<br> date2"." ".$bad_date2. "<br> date3"." ".$bad_date3;
 // It will show: 2020-01-22
 echo "<br>";
 $better_date1 = nice_date($bad_date1, 'Y-m-d');
 $better_date2 = nice_date($bad_date2, 'Y-m-d');
 $better_date3 = nice_date($bad_date3, 'Y-m-d');
 echo "Nice date Format" ." ".$better_date1. " <br> ".$better_date2."<br> ".$better_date3;
 }
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function it shows the output, as shown below.

CodeIgniter Date helper

10. timestamp() Function: It is used to display the elapsed time from some point in the past to the current time.

Syntax

timespan([ $past_time= ‘ ‘ [, $unix_time= ‘’ [, $units
= ‘ ‘  ] ] ]);

Parameters

  • $past_time (int): It takes the past time in UNIX timestamp.
  • $unix_time (int): It takes the current time as input in Unix timestamp.
  • $units (int): It defines the number of units or fields, that you want to show.

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 $past_date = '1579564573';  // it display the yesterday time till now  
 $past_date1 = '1563666635';
 $now = time(); // it take current time in Unix timestamp
 $units = 3;       // no. of units
 $units2 = 5;      
 echo "Display the last point of time till now ";
 echo timespan($past_date, $now, $units); 
 echo "<br> It display the time from 2019-07-21 05:20:35 pm till now ";
 echo timespan($past_date1, $now, $units2); 
 }
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function. It shows the output, as shown below.

CodeIgniter Date helper

11. days_in_month() Function:- It is used to return the number of days in a given month and year.

Syntax

days_in_month( [ $month = ‘ ‘ [. $year = ‘ ‘] ]);

Parameters

  • $month (int): It takes month in numeric.
  • $year (int): It takes year in numeric.

Example: Now create a Tests.php file in application/controller folder and write the following program:

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {   
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 echo "NUMBER OF DAYS in (February, 2020) is ";
 echo days_in_month(02, 2020);  //month and year
 }
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate function, it shows the output, as shown below.

CodeIgniter Date helper

12. date_range() Function: It  is used to create a list of dates within a specified period.

Syntax

date_range( [ $unix_start = ‘ ‘, [, $mixed = ‘ ‘ [,
$is_unix = TRUE [, $format = ‘Y-m-d’] ] ] ]);

Parameters

  • $unix_start (int): It defines the starting date in Unix timestamp.
  • $mixed (int): It defines the ending date of the range in Unix timestamp.
  • $format: It defines the format of date in ‘y-m-d’.

Example: Create a Tests.php file in application/controller folder and write the following program.

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 $rangeBetween  = date_range('2020-01-05', '2020-01-26'); // SET RANGE
 echo "First 22 days between 5-26: ";
 foreach ($rangeBetween as $info)
 {
         echo " ".$info."\t ";
 }
 }
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function, it shows the output, as shown below.

CodeIgniter Date helper

13. timezones(): It is used to take the reference timezone and return the number of hours from the UTC zone.

Syntax

timezones( [$tz = ‘’]);

Where, $tz = It is used to take numeric timezone. For example, UM8, UM6, UP55, UP575, UM1, etc.

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 echo "timezone of india: ";
 echo timezones('UP55');
 }
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function, it shows the output, as shown below.

CodeIgniter Date helper

14. timezone_menu() Function: It is used to create a menu of timezones. It contains one argument that for pre selecting of the particular timezone. For example, if you want to set Indian time as default, you can use timezone_menu(‘UP55’);.

Example: Create a Tests.php file in application/controller folder and write the following program.

Tests.php

<?php
 class Tests extends CI_Controller
  {
 public function getCurrentDate() 
     {  
        $this->load->helper('date');  
         echo "<title> Tutorial And Example </title>"; 
 echo "SET INDIA zone as default ";
 echo timezone_menu('UP55');
 echo "<br/> <br>";
 echo timezone_menu();
 }
 }
 ?>        

When you execute the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/tests/and call the getCurrentDate() function, it shows the output, as shown below.

CodeIgniter Date helper

Related Topics

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.