Date Helper

The Date Helper file contains functions that help you work with dates.

Loading this Helper

This helper is loaded using the following code:

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

Available Functions

The following functions are available:

now([$timezone = NULL])
Parameters:
  • $timezone (string) – Timezone
Returns:

UNIX timestamp

Return type:

int

Returns the current time as a UNIX timestamp, referenced either to your server’s local time or any PHP supported timezone, based on the “time reference” setting in your config file. If you do not intend to set your master time reference to any other PHP supported timezone (which you’ll typically do if you run a site that lets each user set their own timezone settings) there is no benefit to using this function over PHP’s time() function.

echo now('Australia/Victoria');

If a timezone is not provided, it will return time() based on the time_reference setting.

mdate([$datestr = ''[, $time = '']])
Parameters:
  • $datestr (string) – Date string
  • $time (int) – UNIX timestamp
Returns:

MySQL-formatted date

Return type:

string

This function is identical to PHP’s date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign, e.g. %Y %m %d

The benefit of doing dates this way is that you don’t have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.

Example:

$datestring = 'Year: %Y Month: %m Day: %d - %h:%i %a';
$time = time();
echo mdate($datestring, $time);

If a timestamp is not included in the second parameter the current time will be used.

standard_date([$fmt = 'DATE_RFC822'[, $time = NULL]])
<
Parameters:
  • $fmt (string) – Date format
  • $time (int) – UNIX timestamp
Returns:

Formatted date or FALSE on invalid format