日期处理类
常量 | |
---|---|
Date::YEAR |
integer 31556926 |
Date::MONTH |
integer 2629744 |
Date::WEEK |
integer 604800 |
Date::DAY |
integer 86400 |
Date::HOUR |
integer 3600 |
Date::MINUTE |
integer 60 |
Date::MONTHS_LONG |
string(2) "%B" |
Date::MONTHS_SHORT |
string(2) "%b" |
API - Core_Date
- Date::$timestamp_format - Default timestamp format for formatted_time
- Date::$timezone - Timezone for formatted_time
- Date::offset - Returns the offset (in seconds) between two time zones. Use this to
- Date::seconds - Number of seconds in a minute, incrementing by a step. Typically used as
- Date::minutes - Number of minutes in an hour, incrementing by a step. Typically used as
- Date::hours - Number of hours in a day. Typically used as a shortcut for generating a
- Date::ampm - Returns AM or PM, based on a given hour (in 24 hour format).
- Date::adjust - Adjusts a non-24-hour number into a 24-hour number.
- Date::days - Number of days in a given month and year. Typically used as a shortcut
- Date::months - Number of months in a year. Typically used as a shortcut for generating
- Date::years - Returns an array of years between a starting and ending year. By default,
- Date::span - Returns time difference between two timestamps, in human readable format.
- Date::fuzzy_span - Returns the difference between a time and now in a "fuzzy" way.
- Date::unix2dos - Converts a UNIX timestamp to DOS format. There are very few cases where
- Date::dos2unix - Converts a DOS timestamp to UNIX format.There are very few cases where
- Date::formatted_time - Returns a date/time string with the specified timestamp format
Returns the offset (in seconds) between two time zones. Use this to display dates to users in different time zones.
$seconds = Date::offset('America/Chicago', 'GMT');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$remote |
string |
Timezone that to find the offset of | |
$local |
string |
Timezone used as the baseline | null |
$now |
mixed |
UNIX timestamp or date string | null |
integer
Number of seconds in a minute, incrementing by a step. Typically used as a shortcut for generating a list that can used in a form.
$seconds = Date::seconds(); // 01, 02, 03, ..., 58, 59, 60
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$step |
integer |
Amount to increment each step by, 1 to 30 | integer 1 |
$start |
integer |
Start value | integer 0 |
$end |
integer |
End value | integer 60 |
array
A mirrored (foo => foo) array from 1-60.Number of minutes in an hour, incrementing by a step. Typically used as a shortcut for generating a list that can be used in a form.
$minutes = Date::minutes(); // 05, 10, 15, ..., 50, 55, 60
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$step |
integer |
Amount to increment each step by, 1 to 30 | integer 5 |
array
A mirrored (foo => foo) array from 1-60.Number of hours in a day. Typically used as a shortcut for generating a list that can be used in a form.
$hours = Date::hours(); // 01, 02, 03, ..., 10, 11, 12
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$step |
integer |
Amount to increment each step by | integer 1 |
$long |
boolean |
Use 24-hour time | bool false |
$start |
integer |
The hour to start at | null |
array
A mirrored (foo => foo) array from start-12 or start-23.Returns AM or PM, based on a given hour (in 24 hour format).
$type = Date::ampm(12); // PM $type = Date::ampm(1); // AM
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$hour |
integer |
Number of the hour |
string
Adjusts a non-24-hour number into a 24-hour number.
$hour = Date::adjust(3, 'pm'); // 15
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$hour |
integer |
Hour to adjust | |
$ampm |
string |
AM or PM |
string
Number of days in a given month and year. Typically used as a shortcut for generating a list that can be used in a form.
Date::days(4, 2010); // 1, 2, 3, ..., 28, 29, 30
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$month |
integer |
Number of month | |
$year |
integer |
Number of year to check month, defaults to the current year | bool false |
array
A mirrored (foo => foo) array of the days.Number of months in a year. Typically used as a shortcut for generating a list that can be used in a form.
By default a mirrored array of $month_number => $month_number is returned
Date::months(); // aray(1 => 1, 2 => 2, 3 => 3, ..., 12 => 12)
But you can customise this by passing in either Date::MONTHS_LONG
Date::months(Date::MONTHS_LONG); // array(1 => 'January', 2 => 'February', ..., 12 => 'December')
Or Date::MONTHS_SHORT
Date::months(Date::MONTHS_SHORT); // array(1 => 'Jan', 2 => 'Feb', ..., 12 => 'Dec')
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$format |
string |
The format to use for months | null |
array
An array of months based on the specified formatReturns an array of years between a starting and ending year. By default, the the current year - 5 and current year + 5 will be used. Typically used as a shortcut for generating a list that can be used in a form.
$years = Date::years(2000, 2010); // 2000, 2001, ..., 2009, 2010
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$start |
integer |
Starting year (default is current year - 5) | bool false |
$end |
integer |
Ending year (default is current year + 5) | bool false |
array
Returns time difference between two timestamps, in human readable format. If the second timestamp is not given, the current time will be used. Also consider using [Date::fuzzy_span] when displaying a span.
$span = Date::span(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2) $span = Date::span(60, 182, 'minutes'); // 2
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$remote |
integer |
Timestamp to find the span of | |
$local |
integer |
Timestamp to use as the baseline | null |
$output |
string |
Formatting string | string(45) "years,months,weeks,days,hours,minutes,seconds" |
string
when only a single output is requestedarray
associative list of all outputs requestedReturns the difference between a time and now in a "fuzzy" way. Displaying a fuzzy time instead of a date is usually faster to read and understand.
$span = Date::fuzzy_span(time() - 10); // "moments ago" $span = Date::fuzzy_span(time() + 20); // "in moments"
A second parameter is available to manually set the "local" timestamp, however this parameter shouldn't be needed in normal usage and is only included for unit tests
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$timestamp |
integer |
"remote" timestamp | |
$local_timestamp |
integer |
"local" timestamp, defaults to time() | null |
string
Converts a UNIX timestamp to DOS format. There are very few cases where this is needed, but some binary formats use it (eg: zip files.) Converting the other direction is done using {@link Date::dos2unix}.
$dos = Date::unix2dos($unix);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$timestamp |
integer |
UNIX timestamp | bool false |
integer
Converts a DOS timestamp to UNIX format.There are very few cases where this is needed, but some binary formats use it (eg: zip files.) Converting the other direction is done using {@link Date::unix2dos}.
$unix = Date::dos2unix($dos);
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$timestamp |
integer |
DOS timestamp | bool false |
integer
Returns a date/time string with the specified timestamp format
$time = Date::formatted_time('5 minutes ago');
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$datetime_str |
string |
Datetime_str datetime string | string(3) "now" |
$timestamp_format |
string |
Timestamp_format timestamp format | null |
$timezone |
unknown |
null |
string