Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | Date Helper |
| 3 | ########### |
| 4 | |
| 5 | The Date Helper file contains functions that help you work with dates. |
| 6 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 7 | .. contents:: |
| 8 | :local: |
| 9 | |
| 10 | .. raw:: html |
| 11 | |
| 12 | <div class="custom-index container"></div> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
| 14 | Loading this Helper |
| 15 | =================== |
| 16 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 17 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
| 19 | $this->load->helper('date'); |
| 20 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 21 | Available Functions |
| 22 | =================== |
| 23 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | The following functions are available: |
| 25 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 27 | .. function:: now([$timezone = NULL]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 29 | :param string $timezone: Timezone |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 30 | :returns: UNIX timestamp |
| 31 | :rtype: int |
Iban Eguia | 7bf0a4f | 2012-03-27 18:36:15 +0200 | [diff] [blame] | 32 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 33 | Returns the current time as a UNIX timestamp, referenced either to your server's |
| 34 | local time or any PHP suported timezone, based on the "time reference" setting |
| 35 | in your config file. If you do not intend to set your master time reference to |
| 36 | any other PHP supported timezone (which you'll typically do if you run a site |
| 37 | that lets each user set their own timezone settings) there is no benefit to using |
| 38 | this function over PHP's ``time()`` function. |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 39 | :: |
Iban Eguia | 7bf0a4f | 2012-03-27 18:36:15 +0200 | [diff] [blame] | 40 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 41 | echo now('Australia/Victoria'); |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 42 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 43 | If a timezone is not provided, it will return ``time()`` based on the |
| 44 | **time_reference** setting. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 46 | .. function:: mdate([$datestr = ''[, $time = '']]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 47 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 48 | :param string $datestr: Date string |
| 49 | :param int $time: UNIX timestamp |
| 50 | :returns: MySQL-formatted date |
| 51 | :rtype: string |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 52 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 53 | This function is identical to PHP's `date() <http://www.php.net/date>`_ |
| 54 | function, except that it lets you use MySQL style date codes, where each |
| 55 | code letter is preceded with a percent sign, e.g. `%Y %m %d` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 57 | The benefit of doing dates this way is that you don't have to worry |
| 58 | about escaping any characters that are not date codes, as you would |
| 59 | normally have to do with the ``date()`` function. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 61 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 63 | $datestring = 'Year: %Y Month: %m Day: %d - %h:%i %a'; |
| 64 | $time = time(); |
| 65 | echo mdate($datestring, $time); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 67 | If a timestamp is not included in the second parameter the current time |
| 68 | will be used. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 70 | .. function:: standard_date([$fmt = 'DATE_RFC822'[, $time = NULL]]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 71 | |
| 72 | :param string $fmt: Date format |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 73 | :param int $time: UNIX timestamp |
| 74 | :returns: Formatted date or FALSE on invalid format |
| 75 | :rtype: string |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 76 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 77 | Lets you generate a date string in one of several standardized formats. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 79 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 81 | $format = 'DATE_RFC822'; |
| 82 | $time = time(); |
| 83 | echo standard_date($format, $time); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
Andrey Andreev | 8b9dd22 | 2014-01-24 14:41:22 +0200 | [diff] [blame] | 85 | .. note:: This function is DEPRECATED. Use the native ``date()`` combined with |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 86 | `DateTime's format constants |
| 87 | <http://www.php.net/manual/en/class.datetime.php#datetime.constants.types>`_ |
| 88 | instead:: |
Andrey Andreev | ac57033 | 2012-07-04 13:04:10 +0300 | [diff] [blame] | 89 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 90 | echo date(DATE_RFC822, time()); |
Andrey Andreev | ac57033 | 2012-07-04 13:04:10 +0300 | [diff] [blame] | 91 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 92 | **Supported formats:** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 94 | =============== ======================= ====================================== |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 95 | Constant Description Example |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 96 | =============== ======================= ====================================== |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 97 | DATE_ATOM Atom 2005-08-15T16:13:03+0000 |
| 98 | DATE_COOKIE HTTP Cookies Sun, 14 Aug 2005 16:13:03 UTC |
| 99 | DATE_ISO8601 ISO-8601 2005-08-14T16:13:03+00:00 |
| 100 | DATE_RFC822 RFC 822 Sun, 14 Aug 05 16:13:03 UTC |
| 101 | DATE_RFC850 RFC 850 Sunday, 14-Aug-05 16:13:03 UTC |
| 102 | DATE_RFC1036 RFC 1036 Sunday, 14-Aug-05 16:13:03 UTC |
| 103 | DATE_RFC1123 RFC 1123 Sun, 14 Aug 2005 16:13:03 UTC |
| 104 | DATE_RFC2822 RFC 2822 Sun, 14 Aug 2005 16:13:03 +0000 |
| 105 | DATE_RSS RSS Sun, 14 Aug 2005 16:13:03 UTC |
| 106 | DATE_W3C W3C 2005-08-14T16:13:03+0000 |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 107 | =============== ======================= ====================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 109 | .. function:: local_to_gmt([$time = '']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 111 | :param int $time: UNIX timestamp |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 112 | :returns: UNIX timestamp |
| 113 | :rtype: int |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 115 | Takes a UNIX timestamp as input and returns it as GMT. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 117 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 119 | $gmt = local_to_gmt(time()); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 121 | .. function:: gmt_to_local([$time = ''[, $timezone = 'UTC'[, $dst = FALSE]]]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 122 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 123 | :param int $time: UNIX timestamp |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 124 | :param string $timezone: Timezone |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 125 | :param bool $dst: Whether DST is active |
| 126 | :returns: UNIX timestamp |
| 127 | :rtype: int |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 128 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 129 | Takes a UNIX timestamp (referenced to GMT) as input, and converts it to |
| 130 | a localized timestamp based on the timezone and Daylight Saving Time |
| 131 | submitted. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 133 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 135 | $timestamp = 1140153693; |
| 136 | $timezone = 'UM8'; |
| 137 | $daylight_saving = TRUE; |
| 138 | echo gmt_to_local($timestamp, $timezone, $daylight_saving); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
| 140 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 141 | .. note:: For a list of timezones see the reference at the bottom of this page. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 143 | .. function:: mysql_to_unix([$time = '']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 145 | :param string $time: MySQL timestamp |
| 146 | :returns: UNIX timestamp |
| 147 | :rtype: int |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 149 | Takes a MySQL Timestamp as input and returns it as a UNIX timestamp. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 151 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 153 | $unix = mysql_to_unix('20061124092345'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 155 | .. function:: unix_to_human([$time = ''[, $seconds = FALSE[, $fmt = 'us']]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 156 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 157 | :param int $time: UNIX timestamp |
| 158 | :param bool $seconds: Whether to show seconds |
| 159 | :param string $fmt: format (us or euro) |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 160 | :returns: Formatted date |
| 161 | :rtype: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 163 | Takes a UNIX timestamp as input and returns it in a human readable |
| 164 | format with this prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 166 | YYYY-MM-DD HH:MM:SS AM/PM |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 168 | This can be useful if you need to display a date in a form field for |
| 169 | submission. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 171 | The time can be formatted with or without seconds, and it can be set to |
| 172 | European or US format. If only the timestamp is submitted it will return |
| 173 | the time without seconds formatted for the U.S. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 174 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 175 | Examples:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 177 | $now = time(); |
| 178 | echo unix_to_human($now); // U.S. time, no seconds |
| 179 | echo unix_to_human($now, TRUE, 'us'); // U.S. time with seconds |
| 180 | echo unix_to_human($now, TRUE, 'eu'); // Euro time with seconds |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 181 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 182 | .. function:: human_to_unix([$datestr = '']) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 183 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 184 | :param int $datestr: Date string |
| 185 | :returns: UNIX timestamp or FALSE on failure |
| 186 | :rtype: int |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 187 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 188 | The opposite of the :func:`unix_to_time()` function. Takes a "human" |
| 189 | time as input and returns it as a UNIX timestamp. This is useful if you |
| 190 | accept "human" formatted dates submitted via a form. Returns boolean FALSE |
| 191 | date string passed to it is not formatted as indicated above. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 192 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 193 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 194 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 195 | $now = time(); |
| 196 | $human = unix_to_human($now); |
| 197 | $unix = human_to_unix($human); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 199 | .. function:: nice_date([$bad_date = ''[, $format = FALSE]]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 200 | |
| 201 | :param int $bad_date: The terribly formatted date-like string |
| 202 | :param string $format: Date format to return (same as PHP's ``date()`` function) |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 203 | :returns: Formatted date |
| 204 | :rtype: string |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 205 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 206 | This function can take a number poorly-formed date formats and convert |
| 207 | them into something useful. It also accepts well-formed dates. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 209 | The function will return a UNIX timestamp by default. You can, optionally, |
| 210 | pass a format string (the same type as the PHP ``date()`` function accepts) |
| 211 | as the second parameter. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 212 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 213 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 214 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 215 | $bad_date = '199605'; |
| 216 | // Should Produce: 1996-05-01 |
| 217 | $better_date = nice_date($bad_date, 'Y-m-d'); |
Andrey Andreev | c275b23 | 2012-06-15 16:13:17 +0300 | [diff] [blame] | 218 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 219 | $bad_date = '9-11-2001'; |
| 220 | // Should Produce: 2001-09-11 |
| 221 | $better_date = nice_date($bad_date, 'Y-m-d'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 222 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 223 | .. function:: timespan([$seconds = 1[, $time = ''[, $units = '']]]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 224 | |
| 225 | :param int $seconds: Number of seconds |
| 226 | :param string $time: UNIX timestamp |
| 227 | :param int $units: Number of time units to display |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 228 | :returns: Formatted time difference |
| 229 | :rtype: string |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 230 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 231 | Formats a UNIX timestamp so that is appears similar to this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 232 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 233 | 1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 234 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 235 | The first parameter must contain a UNIX timestamp. |
| 236 | The second parameter must contain a timestamp that is greater that the |
| 237 | first timestamp. |
| 238 | The thirdparameter is optional and limits the number of time units to display. |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 239 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 240 | If the second parameter empty, the current time will be used. |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 241 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 242 | The most common purpose for this function is to show how much time has |
| 243 | elapsed from some point in time in the past to now. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 244 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 245 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 246 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 247 | $post_date = '1079621429'; |
| 248 | $now = time(); |
| 249 | $units = 2; |
| 250 | echo timespan($post_date, $now, $units); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 251 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 252 | .. note:: The text generated by this function is found in the following language |
| 253 | file: `language/<your_lang>/date_lang.php` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 254 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 255 | .. function:: days_in_month([$month = 0[, $year = '']]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 256 | |
| 257 | :param int $month: a numeric month |
| 258 | :param int $year: a numeric year |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 259 | :returns: Count of days in the specified month |
| 260 | :rtype: int |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 261 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 262 | Returns the number of days in a given month/year. Takes leap years into |
| 263 | account. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 264 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 265 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 266 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 267 | echo days_in_month(06, 2005); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 268 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 269 | If the second parameter is empty, the current year will be used. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 270 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 271 | .. function:: date_range([$unix_start = ''[, $mixed = ''[, $is_unix = TRUE[, $format = 'Y-m-d']]]]) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 272 | |
| 273 | :param int $unix_start: UNIX timestamp of the range start date |
| 274 | :param int $mixed: UNIX timestamp of the range end date or interval in days |
| 275 | :param bool $is_unix: set to FALSE if $mixed is not a timestamp |
| 276 | :param string $format: Output date format, same as in ``date()`` |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 277 | :returns: An array of dates |
| 278 | :rtype: array |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 279 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 280 | Returns a list of dates within a specified period. |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 281 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 282 | Example:: |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 283 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 284 | $range = date_range('2012-01-01', '2012-01-15'); |
| 285 | echo "First 15 days of 2012:"; |
| 286 | foreach ($range as $date) |
| 287 | { |
| 288 | echo $date."\n"; |
| 289 | } |
Andrey Andreev | 2139ecd | 2012-01-11 23:58:50 +0200 | [diff] [blame] | 290 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 291 | .. function:: timezones([$tz = '']) |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 292 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 293 | :param string $tz: A numeric timezone |
| 294 | :returns: Hour difference from UTC |
| 295 | :rtype: int |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 296 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 297 | Takes a timezone reference (for a list of valid timezones, see the |
| 298 | "Timezone Reference" below) and returns the number of hours offset from |
| 299 | UTC. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 300 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 301 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 302 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 303 | echo timezones('UM5'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 304 | |
| 305 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 306 | This function is useful when used with :func:`timezone_menu()`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 307 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 308 | .. function:: timezone_menu([$default = 'UTC'[, $class = ''[, $name = 'timezones'[, $attributes = '']]]]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 309 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 310 | :param string $default: Timezone |
| 311 | :param string $class: Class name |
| 312 | :param string $name: Menu name |
| 313 | :param mixed $attributes: HTML attributes |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 314 | :returns: HTML drop down menu with time zones |
| 315 | :rtype: string |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 316 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 317 | Generates a pull-down menu of timezones, like this one: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 318 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 319 | .. raw:: html |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 320 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 321 | <form action="#"> |
| 322 | <select name="timezones"> |
| 323 | <option value='UM12'>(UTC -12:00) Baker/Howland Island</option> |
| 324 | <option value='UM11'>(UTC -11:00) Samoa Time Zone, Niue</option> |
| 325 | <option value='UM10'>(UTC -10:00) Hawaii-Aleutian Standard Time, Cook Islands, Tahiti</option> |
| 326 | <option value='UM95'>(UTC -9:30) Marquesas Islands</option> |
| 327 | <option value='UM9'>(UTC -9:00) Alaska Standard Time, Gambier Islands</option> |
| 328 | <option value='UM8'>(UTC -8:00) Pacific Standard Time, Clipperton Island</option> |
| 329 | <option value='UM7'>(UTC -7:00) Mountain Standard Time</option> |
| 330 | <option value='UM6'>(UTC -6:00) Central Standard Time</option> |
| 331 | <option value='UM5'>(UTC -5:00) Eastern Standard Time, Western Caribbean Standard Time</option> |
| 332 | <option value='UM45'>(UTC -4:30) Venezuelan Standard Time</option> |
| 333 | <option value='UM4'>(UTC -4:00) Atlantic Standard Time, Eastern Caribbean Standard Time</option> |
| 334 | <option value='UM35'>(UTC -3:30) Newfoundland Standard Time</option> |
| 335 | <option value='UM3'>(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay</option> |
| 336 | <option value='UM2'>(UTC -2:00) South Georgia/South Sandwich Islands</option> |
| 337 | <option value='UM1'>(UTC -1:00) Azores, Cape Verde Islands</option> |
| 338 | <option value='UTC' selected='selected'>(UTC) Greenwich Mean Time, Western European Time</option> |
| 339 | <option value='UP1'>(UTC +1:00) Central European Time, West Africa Time</option> |
| 340 | <option value='UP2'>(UTC +2:00) Central Africa Time, Eastern European Time, Kaliningrad Time</option> |
| 341 | <option value='UP3'>(UTC +3:00) Moscow Time, East Africa Time</option> |
| 342 | <option value='UP35'>(UTC +3:30) Iran Standard Time</option> |
| 343 | <option value='UP4'>(UTC +4:00) Azerbaijan Standard Time, Samara Time</option> |
| 344 | <option value='UP45'>(UTC +4:30) Afghanistan</option> |
| 345 | <option value='UP5'>(UTC +5:00) Pakistan Standard Time, Yekaterinburg Time</option> |
| 346 | <option value='UP55'>(UTC +5:30) Indian Standard Time, Sri Lanka Time</option> |
| 347 | <option value='UP575'>(UTC +5:45) Nepal Time</option> |
| 348 | <option value='UP6'>(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time</option> |
| 349 | <option value='UP65'>(UTC +6:30) Cocos Islands, Myanmar</option> |
| 350 | <option value='UP7'>(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam</option> |
| 351 | <option value='UP8'>(UTC +8:00) Australian Western Standard Time, Beijing Time, Irkutsk Time</option> |
| 352 | <option value='UP875'>(UTC +8:45) Australian Central Western Standard Time</option> |
| 353 | <option value='UP9'>(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk Time</option> |
| 354 | <option value='UP95'>(UTC +9:30) Australian Central Standard Time</option> |
| 355 | <option value='UP10'>(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time</option> |
| 356 | <option value='UP105'>(UTC +10:30) Lord Howe Island</option> |
| 357 | <option value='UP11'>(UTC +11:00) Magadan Time, Solomon Islands, Vanuatu</option> |
| 358 | <option value='UP115'>(UTC +11:30) Norfolk Island</option> |
| 359 | <option value='UP12'>(UTC +12:00) Fiji, Gilbert Islands, Kamchatka Time, New Zealand Standard Time</option> |
| 360 | <option value='UP1275'>(UTC +12:45) Chatham Islands Standard Time</option> |
| 361 | <option value='UP13'>(UTC +13:00) Phoenix Islands Time, Tonga</option> |
| 362 | <option value='UP14'>(UTC +14:00) Line Islands</option> |
| 363 | </select> |
| 364 | </form> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 365 | |
| 366 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 367 | This menu is useful if you run a membership site in which your users are |
| 368 | allowed to set their local timezone value. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 369 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 370 | The first parameter lets you set the "selected" state of the menu. For |
| 371 | example, to set Pacific time as the default you will do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 372 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 373 | echo timezone_menu('UM8'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 374 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 375 | Please see the timezone reference below to see the values of this menu. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 376 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 377 | The second parameter lets you set a CSS class name for the menu. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 378 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 379 | The fourth parameter lets you set one or more attributes on the generated select tag. |
Mat Whitney | 7540ded | 2012-06-22 12:02:10 -0700 | [diff] [blame] | 380 | |
Derek Jones | a4db432 | 2013-07-20 08:59:59 -0700 | [diff] [blame] | 381 | .. note:: The text contained in the menu is found in the following |
| 382 | language file: `language/<your_lang>/date_lang.php` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 383 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 384 | Timezone Reference |
| 385 | ================== |
| 386 | |
| 387 | The following table indicates each timezone and its location. |
| 388 | |
Kwaan Online | e90b684 | 2012-01-31 10:15:30 +0000 | [diff] [blame] | 389 | Note some of the location lists have been abridged for clarity and formatting. |
| 390 | |
Andrey Andreev | 3de130c | 2014-02-07 23:31:49 +0200 | [diff] [blame^] | 391 | =========== ===================================================================== |
| 392 | Time Zone Location |
| 393 | =========== ===================================================================== |
| 394 | UM12 (UTC - 12:00) Baker/Howland Island |
| 395 | UM11 (UTC - 11:00) Samoa Time Zone, Niue |
| 396 | UM10 (UTC - 10:00) Hawaii-Aleutian Standard Time, Cook Islands |
| 397 | UM95 (UTC - 09:30) Marquesas Islands |
| 398 | UM9 (UTC - 09:00) Alaska Standard Time, Gambier Islands |
| 399 | UM8 (UTC - 08:00) Pacific Standard Time, Clipperton Island |
| 400 | UM7 (UTC - 11:00) Mountain Standard Time |
| 401 | UM6 (UTC - 06:00) Central Standard Time |
| 402 | UM5 (UTC - 05:00) Eastern Standard Time, Western Caribbean |
| 403 | UM45 (UTC - 04:30) Venezuelan Standard Time |
| 404 | UM4 (UTC - 04:00) Atlantic Standard Time, Eastern Caribbean |
| 405 | UM35 (UTC - 03:30) Newfoundland Standard Time |
| 406 | UM3 (UTC - 03:00) Argentina, Brazil, French Guiana, Uruguay |
| 407 | UM2 (UTC - 02:00) South Georgia/South Sandwich Islands |
| 408 | UM1 (UTC -1:00) Azores, Cape Verde Islands |
| 409 | UTC (UTC) Greenwich Mean Time, Western European Time |
| 410 | UP1 (UTC +1:00) Central European Time, West Africa Time |
| 411 | UP2 (UTC +2:00) Central Africa Time, Eastern European Time |
| 412 | UP3 (UTC +3:00) Moscow Time, East Africa Time |
| 413 | UP35 (UTC +3:30) Iran Standard Time |
| 414 | UP4 (UTC +4:00) Azerbaijan Standard Time, Samara Time |
| 415 | UP45 (UTC +4:30) Afghanistan |
| 416 | UP5 (UTC +5:00) Pakistan Standard Time, Yekaterinburg Time |
| 417 | UP55 (UTC +5:30) Indian Standard Time, Sri Lanka Time |
| 418 | UP575 (UTC +5:45) Nepal Time |
| 419 | UP6 (UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time |
| 420 | UP65 (UTC +6:30) Cocos Islands, Myanmar |
| 421 | UP7 (UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam |
| 422 | UP8 (UTC +8:00) Australian Western Standard Time, Beijing Time |
| 423 | UP875 (UTC +8:45) Australian Central Western Standard Time |
| 424 | UP9 (UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk |
| 425 | UP95 (UTC +9:30) Australian Central Standard Time |
| 426 | UP10 (UTC +10:00) Australian Eastern Standard Time, Vladivostok Time |
| 427 | UP105 (UTC +10:30) Lord Howe Island |
| 428 | UP11 (UTC +11:00) Magadan Time, Solomon Islands, Vanuatu |
| 429 | UP115 (UTC +11:30) Norfolk Island |
| 430 | UP12 (UTC +12:00) Fiji, Gilbert Islands, Kamchatka, New Zealand |
| 431 | UP1275 (UTC +12:45) Chatham Islands Standard Time |
| 432 | UP13 (UTC +13:00) Phoenix Islands Time, Tonga |
| 433 | UP14 (UTC +14:00) Line Islands |
Andrey Andreev | 58f677f | 2013-07-16 11:01:37 +0300 | [diff] [blame] | 434 | =========== ===================================================================== |