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:

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

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

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

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

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

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

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

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

Tests.php

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

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

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

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

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

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

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

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

Tests.php

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

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

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

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

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

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:

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

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.

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

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

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

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

Pin It on Pinterest

Share This