CodeIgniter Date helper
×

CodeIgniter Date helper

2820

Date helper contains functions that help us to work with dates.


Related Topic:

Codeigniter Interview Questions

Loading this Helper:

Use the given line in your controller or model file to load the Date helper.

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

The following functions are available in this helper:

1 now(): This function is used to get the current time referenced either to your server’s local time or any PHP supported timezone. Time reference depends on the "time reference" setting in your application/config/config.php file.

Syntax:

now([$timezone = NULL]);

Parameter Description:

  • $timezone (string) – Timezone

The return type of this function is int and it returns a UNIX timestamp.

Example:

Step 1 Open the application/controllers directory and create a new controller file Now_controller.php

<?php
class Now_controller extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
echo now('Australia/Victoria');
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Now_controller


2 mdate(): This function is same as the PHP native function date(). The only difference is that it provides the facility to use MySQL style date codes, where each code letter is preceded with a percent sign, e.g. %Y %m %d.

Syntax:

mdate([$datestr = ''[, $time = '']]);

Parameter Description:

  • $datestr (string) – Date string
  • $time (int)UNIX timestamp

The return type of this function is a string and it returns a MySQL-formatted date.

Example:

Step 1 Open the application/controllers directory and create a new controller file Mdate_controller.php

<?php
class Mdate_controller extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$datestring = 'Year: %Y Month: %m Day: %d - %h:%i %a';
$time = time();
echo mdate($datestring, $time);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Mdate_controller


3 standard_date(): This function is used to generate the date string in different standardized formats provided by the user.

Some formats supported by this function are:

Constant
Description
Example
DATE_ATOM
Atom
2005-08-15T16:13:03+0000
DATE_COOKIE
HTTP Cookies
Sun, 14 Aug 2005 16:13:03 UTC
DATE_ISO8601
ISO-8601
2005-08-14T16:13:03+00:00
DATE_RFC822
RFC 822
Sun, 14 Aug 05 16:13:03 UTC
DATE_RFC850
RFC 850
Sunday, 14-Aug-05 16:13:03 UTC
DATE_RFC1036
RFC 1036
Sunday, 14-Aug-05 16:13:03 UTC
DATE_RFC1123
RFC 1123
Sun, 14 Aug 2005 16:13:03 UTC
DATE_RFC2822
RFC 2822
Sun, 14 Aug 2005 16:13:03 +0000
DATE_RSS
RSS
Sun, 14 Aug 2005 16:13:03 UTC
DATE_W3C
W3C
2005-08-14T16:13:03+0000

Syntax:

standard_date([$fmt = 'DATE_RFC822'[, $time = NULL]]);

Parameter Description:

  • $fmt (string) – Date format
  • $time (int) – UNIX timestamp

The return type of this function is a string and it returns formatted date or FALSE on the invalid format.

Example:

Step 1 Open the application/controllers directory and create a new controller file Standard_controller.php

<?php
class Standard_controller extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$format = 'DATE_RFC1036';
$time = time();
echo standard_date($format, $time);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Standard_controller


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

Syntax:

local_to_gmt([$time = '']);

Parameter Description:

  • $time (int) – UNIX timestamp

The return type of this function is int and it returns a UNIX timestamp.

Example:

Step 1 Open the application/controllers directory and create a new controller file Local_to_gmt_controller.php

<?php
class Local_to_gmt_controller extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$gmt = local_to_gmt(time());
echo $gmt;
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Local_to_gmt_controller


5 gmt_to_local(): This function is used to convert a UNIX timestamp (referenced to GMT) into a localized timestamp based on the timezone and Daylight Saving Time submitted.

Syntax:

gmt_to_local([$time = ''[, $timezone = 'UTC'[, $dst = FALSE]]]);

Parameter Description:

  • $time (int) – UNIX timestamp
  • $timezone (string) – Timezone
  • $dst (bool) – Whether DST is active

The return type of this function is int and it returns a UNIX timestamp.

Example:

Step 1 Open the application/controllers directory and create a new controller file Gmt_to_local_controller.php

<?php
class Gmt_to_local_controller extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$timestamp = 1140153693;
$timezone = 'UM8';
$daylight_saving = TRUE;
echo gmt_to_local($timestamp, $timezone, $daylight_saving);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Gmt_to_local_controller


6 mysql_to_unix(): This function is used to convert MySQL Timestamp into a UNIX timestamp.

Syntax:

mysql_to_unix([$time = '']);

Parameter Description:

  • $time (string) – MySQL timestamp

The return type of this function is int and it returns a UNIX timestamp.

Example:

Step 1 Open application/controllers directory and create a new controller file Mysql_to_unix_controller.php

<?php
class Mysql_to_unix_controller extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$unix = mysql_to_unix('20061124092345');
echo $unix;
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Mysql_to_unix_controller


7 unix_to_human(): This function is used to convert a UNIX timestamp into a human-readable format with the given prototype:

YYYY-MM-DD HH:MM:SS AM/PM

Syntax:

unix_to_human([$time = ''[, $seconds = FALSE[, $fmt = 'us']]]);

Parameter Description:

  • $time (int) – UNIX timestamp
  • $seconds (bool) – Whether to show seconds
  • $fmt (string) – format (us or euro)

The return type of this function is a string and it returns a formatted date.

Example:

Step 1 Open application/controllers directory and create a new controller file Unix_to_human.php

<?php
class Unix_to_human extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$now = time();
echo unix_to_human($now); // U.S. time, no seconds
echo '<br>'.unix_to_human($now, TRUE, 'us'); // U.S. time with seconds
echo '<br>'.unix_to_human($now, TRUE, 'eu'); // Euro time with seconds
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Unix_to_human


8 human_to_unix(): This function takes human time as input and converts it into a UNIX timestamp.

Syntax:

human_to_unix([$datestr = '']);

Parameter Description:

  • $datestr (int) – Date string

The return type of this function is int and it returns UNIX timestamp or FALSE on failure.

Example:

Step 1 Open application/controllers directory and create a new controller file Human_to_unix.php

<?php
class Human_to_unix extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$now = time();
$human = unix_to_human($now);
$unix = human_to_unix($human);
echo "Human readable date: $human <br>";
echo "UNIX timestamp :$unix";
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Human_to_unix


9 nice_date(): This function is used to convert a number of poorly-formed date formats into something useful.

Syntax:

nice_date([$bad_date = ''[, $format = FALSE]]);

Parameter Description:

  • $bad_date (int) – The terribly formatted date-like string
  • $format (string) – Date format to return (same as PHP’s date() function)

The return type of this function is a string and it returns a formatted date.

Example:

Step 1 Open the application/controllers directory and create a new controller file Nice_date.php

<?php
class Nice_date extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$bad_date = '9-11-2001';
// Should Produce: 2001-09-11
$better_date = nice_date($bad_date, 'Y-m-d');
echo "bad date: $bad_date <br>";
echo "good date: $better_date";
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Nice_date


10 timespan(): This function is used to convert a UNIX timestamp into the given format.

1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes

Syntax:

timespan([$seconds = 1[, $time = ''[, $units = '']]]);

Parameter Description:

  • $seconds (int) – Number of seconds
  • $time (string) – UNIX timestamp
  • $units (int) – Number of time units to display

The return type of this function is a string and it returns a formatted time difference.

Example:

Step 1 Open application/controllers directory and create a new controller file Timespan.php

<?php
class Timespan extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$post_date = '1079621429';
$now = time();
$units = 2;
echo "Time in 2 units: ".timespan($post_date, $now, $units)."<br>";
$units = 4;
echo "Time in 4 units: ".timespan($post_date, $now, $units);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Timespan


11 days_in_month(): This function is used to returns the number of days in a given month/year. It is also considered a leap year concept. Working of this function is same as PHP native function cal_days_in_month().

Syntax:

days_in_month([$month = 0[, $year = '']]);

Parameter Description:

  • $month (int) – a numeric month
  • $year (int) – a numeric year

The return type of this function is a string and it returns the Count of days in the specified month.

Example:

Step 1 Open the application/controllers directory and create a new controller file Days_in_month.php

<?php
class Days_in_month extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
echo days_in_month(06, 2020);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Days_in_month


12 date_range(): This function is used to get a list of dates within a specified period.

Syntax:

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

Parameter Description:

  • $unix_start (int) – UNIX timestamp of the range start date
  • $mixed (int) – UNIX timestamp of the range end date or interval in days
  • $is_unix (bool) – set to FALSE if $mixed is not a timestamp
  • $format (string) – Output date format, same as in date()

The return type of this function is an array and it returns an array of dates.

Example:

Step 1 Open the application/controllers directory and create a new controller file Date_range.php

<?php
class Date_range extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
$range = date_range('2021-01-01', '2021-01-15');
echo "First 15 days of 2021:";
foreach ($range as $date)
{
echo $date."<br>";
}
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Date_range


13 timezones(): This function takes a timezone reference as input and returns the number of hours offset from UTC.

Syntax:

timezones([$tz = '']);

Parameter Description:

  • $tz (string) – A numeric timezone

The return type of this function is int and it returns an hour difference from UTC.

Example:

Step 1 Open the application/controllers directory and create a new controller file Timezone.php

<?php
class Timezone extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
echo timezones('UM5');
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Date_range


14 timezone_menu(): This function is used to generates a pull-down menu of timezones. This menu is most commonly used in a membership site in which your users are allowed to set their local timezone value.

Syntax:

timezone_menu([$default = 'UTC'[, $class = ''[, $name = 'timezones'[, $attributes = '']]]]);

Parameter Description:

  • $default (string) – Timezone
  • $class (string) – Class name
  • $name (string) – Menu name
  • $attributes (mixed)HTML attributes

The return type of this function is a string and it returns HTML drop-down menu with time zones.

Example:

Step 1 Open the application/controllers directory and create a new controller file Timezone_menu.php

<?php
class Timezone_menu extends CI_Controller {

public function index() {
$this->load->helper('date'); //load date helper
echo timezone_menu('UM8');
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Timezone_menu

Timezone Reference:

Different timezones and their locations are listed below:

Time Zone
Location
UM12
(UTC - 12:00) Baker/Howland Island
UM11
(UTC - 11:00) Samoa Time Zone, Niue
UM10
(UTC - 10:00) Hawaii-Aleutian Standard Time, Cook Islands
UM95
(UTC - 09:30) Marquesas Islands
UM9
(UTC - 09:00) Alaska Standard Time, Gambier Islands
UM8
(UTC - 08:00) Pacific Standard Time, Clipperton Island
UM7
(UTC - 07:00) Mountain Standard Time
UM6
(UTC - 06:00) Central Standard Time
UM5
(UTC - 05:00) Eastern Standard Time, Western Caribbean
UM45
(UTC - 04:30) Venezuelan Standard Time
UM4
(UTC - 04:00) Atlantic Standard Time, Eastern Caribbean
UM35
(UTC - 03:30) Newfoundland Standard Time
UM3
(UTC - 03:00) Argentina, Brazil, French Guiana, Uruguay
UM2
(UTC - 02:00) South Georgia/South Sandwich Islands
UM1
(UTC -1:00) Azores, Cape Verde Islands
UTC
(UTC) Greenwich Mean Time, Western European Time
UP1
(UTC +1:00) Central European Time, West Africa Time
UP2
(UTC +2:00) Central Africa Time, Eastern European Time
UP3
(UTC +3:00) Moscow Time, East Africa Time
UP35
(UTC +3:30) Iran Standard Time
UP4
(UTC +4:00) Azerbaijan Standard Time, Samara Time
UP45
(UTC +4:30) Afghanistan
UP5
(UTC +5:00) Pakistan Standard Time, Yekaterinburg Time
UP55
(UTC +5:30) Indian Standard Time, Sri Lanka Time
UP575
(UTC +5:45) Nepal Time
UP6
(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time
UP65
(UTC +6:30) Cocos Islands, Myanmar
UP7
(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam
UP8
(UTC +8:00) Australian Western Standard Time, Beijing Time
UP875
(UTC +8:45) Australian Central Western Standard Time
UP9
(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk
UP95
(UTC +9:30) Australian Central Standard Time
UP10
(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time
UP105
(UTC +10:30) Lord Howe Island
UP11
(UTC +11:00) Srednekolymsk Time, Solomon Islands, Vanuatu
UP115
(UTC +11:30) Norfolk Island
UP12
(UTC +12:00) Fiji, Gilbert Islands, Kamchatka, New Zealand
UP1275
(UTC +12:45) Chatham Islands Standard Time
UP13
(UTC +13:00) Phoenix Islands Time, Tonga
UP14
(UTC +14:00) Line Islands


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments