Date Helper
Codeigniter Date Helper : Codeigniter provides the helper which is used to work with dates. Here in this tutorial we are going to explain how you can use date helper in Codeigniter. You can use our online editor run and see the output of the example.
Date Helper in Codeigniter
Let us go step by step –
Load Date Helper
First of all load helper(data) before using it. You can the helper(date) simply as below-
Codeigniter Date Helper: Example
$this->load->helper('date'); |
After Loading this helper now you are able to use it’s functions.
Functions of Date Helper
Following functions are present in the date helper.
now Function
This function returns the current time as a UNIX timestamp, referenced by the server’s local time or any other PHP suported timezone based upon the time reference setting in config.
Codeigniter now function syntax
now($timezone = NULL) |
Parameters:
- $timezone (string) – Timezone
- Returns (int) : UNIX timestamp
Now Function Example :
If $timeZone = ‘America/Costa_Rica’; is not passed it will give the time() based on the time_reference settings.
2. mdate Function
This function is somehow similar to the date() function. In addition it enables you to use this function in mysql style date function.
Codeigniter mdate function syntax
mdate($datestr = '', $time = '') |
Parameters:
- $datestr (string) – Date String
- $time (int) – Unix Timestamp
- Returns (int) : String.
mdate Function Example :
Codeigniter mdate function syntax
$datestr = ' %Y - %m - %d - %h:%i %a'; $time = time(); echo mdate($datestr, $time); |
More Functions
Let’s have look over more functions and example.
3. standard_date Function
Codeigniter standard_date function syntax
standard_date($format = 'DATE_RFC822', $time = NULL) |
Parameters:
- $format (string) – Date Format
- $time (int) – Unix Timestamp
- Returns (int) : String.
standard_date Function Example :
Codeigniter standard_date function syntax
$datestr = ' %Y - %m - %d - %h:%i %a'; $time = time(); echo mdate($datestr, $time); |
4. local_to_gmt Function
This function converts the UNIX timestamp into GMT.
Codeigniter local_to_gmt function syntax
local_to_gmt($time = NULL) |
Parameters:
- Returns (int) : Int.
$time (int) – Unix Timestamp
Example :
5. gmt_to_local Function
This function converts GMT timestamp into the localized UNIX timestamp.
Codeigniter gmt_to_local function syntax
gmt_to_local($time = NULL) |
Parameters:
- Returns (int) : Int.
$time (int) – Unix Timestamp
6. mysql_to_unix Function
This function converts mysql timestamp into the UNIX timestamp.
Codeigniter mysql_to_unix function syntax
mysql_to_unix($time = NULL) |
Parameters:
- Returns (int) : Int.
$time (int) – Unix Timestamp
7. unix_to_human Function
This function converts UNIX timestamp to human date format.
Codeigniter unix_to_human function syntax
unix_to_human($time = '', $seconds = FALSE, $format = 'us') |
Parameters:
- $time (int) – Unix Timestamp
- $seconds (bool) – Pass true to show seconds else false.
- $format (string) – String Format.
- Returns (String) : String.
8. human_to_unix Function
This function date into the UNIX timestamp.
Codeigniter human_to_unix function syntax
human_to_unix($dateString) |
Parameters:
- $dateString – Date String
- Returns (Int) : int.
9. nice_date Function
This function takes bad date string and converts it into the proper formatted date..
Codeigniter nice_date function syntax
nice_date($badDate, $format = FALSE) |
Parameters:
- $badDate – Date String not in proper format.
- $format – Format To Be returned.
- Returns (Date) : formatted date.
Example
$badDate = '199005'; $date = nice_date($badDate, 'Y-m-d'); // Will give: 1990-05-01
10. timespan Function
This function formats the UNIX timestamp date..
Codeigniter timespan function syntax
timespan($seconds = 1, $time = '', $units = '') |
Parameters:
- $seconds – No Of seconds.
- $time – UNIX timestamp.
- $units – time unit to return.
- Returns (Date) : formatted date.
11. days_in_month Function
This function returns the number of days in specified year.
Codeigniter days_in_month function syntax
days_in_month($month = 0, $year = '') |
Parameters:
- $month – Month.
- $year – Year.
- Returns (Days) : No of days.
Example
echo days_in_month(08, 2006);
12. date_range Function
This function returns the array of dates between two specified dates.
Codeigniter date_range function syntax
date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d') |
Parameters:
- $unix_start – UNIX timestamp start date.
- $mixed – UNIX timestamp of the range end date.
- $is_unix – This if set to FALSE if $mixed is not a timestamp.
- $format – Output format.
- Returns (Dates) : Returns all dates within the range.
Example
$dateRange = date_range('2014-03-01', '2014-01-20'); print_r($dateRange); // will give you all dates between the range.
13. timezones Function
This function returns the returns the number of hours offset from UTC using the timestamp.
Codeigniter timezones function syntax
timezones($timezone) |
Parameters:
- $timezone – Numeric timezone.
- Returns (int) : Returns hours diff.
14. timezone_menu Function
This function is used to create the dropdown menu of timezones.
Codeigniter timezone_menu function syntax
timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '') |
Parameters:
- $default (string) – Timezone
- $class (string) – Class name
- $name (string) – Menu name
- $attributes (mixed) – HTML attributes
- Returns (HTML) : Returns HTML Dropdown menu of timezones.
Advertisements