PHP Класс Piwik\Date

### Performance concerns The helper methods in this class are instance methods and thus Date instances need to be constructed before they can be used. The memory allocation can result in noticeable performance degradation if you construct thousands of Date instances, say, in a loop. ### Examples **Basic usage** $date = Date::factory('2007-07-24 14:04:24', 'EST'); $date->addHour(5); echo $date->getLocalized("EEE, d. MMM y 'at' HH:mm:ss");
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$timestamp integer | null The returned timestamp via getTimestamp() will have the conversion applied
$timezone string Timezone will only affect the returned timestamp via getTimestamp()
$tokens

Открытые методы

Метод Описание
__toString ( ) : string See {@link toString()}.
addDay ( integer $n ) : Date Adds $n days to $this date and returns the result in a new Date.
addHour ( integer $n ) : Date Adds $n hours to $this date and returns the result in a new Date.
addHourTo ( integer $timestamp, number $n ) : integer Adds N number of hours to a UNIX timestamp and returns the result. Using this static function instead of {@link addHour()} will be faster since a Date instance does not have to be created.
addPeriod ( integer $n, string $period ) : Date Adds a period to $this date and returns the result in a new Date instance.
adjustForTimezone ( integer $timestamp, string $timezone ) : integer Converts a timestamp from UTC to a timezone.
compareMonth ( Date $date ) : integer Performs three-way comparison of the month of the current date against the given $date's month.
compareWeek ( Date $date ) : integer Performs three-way comparison of the week of the current date against the given $date's week.
compareYear ( Date $date ) : integer Performs three-way comparison of the month of the current date against the given $date's year.
factory ( string | integer $dateString, string $timezone = null ) : Date Creates a new Date instance using a string datetime value. The timezone of the Date result will be in UTC.
getDateEndUTC ( ) : string Returns the end of the day of the current timestamp in UTC. For example, if the current timestamp is '2007-07-24 14:03:24' in UTC, the result will be '2007-07-24 23:59:59'.
getDateStartUTC ( ) : string Returns the start of the day of the current timestamp in UTC. For example, if the current timestamp is '2007-07-24 14:04:24' in UTC, the result will be '2007-07-24'.
getDatetime ( ) : string Returns the current timestamp as a string with the following format: 'YYYY-MM-DD HH:MM:SS'.
getDatetimeFromTimestamp ( integer $timestamp ) : string Returns the date in the "Y-m-d H:i:s" PHP format
getHourUTC ( ) : string Returns the current hour in UTC timezone.
getLocalized ( string $template ) : string Returns a localized date string using the given template.
getTimestamp ( ) : integer Returns the unix timestamp of the date in UTC, converted from the current timestamp timezone.
getTimestampUTC ( ) : integer Returns the Unix timestamp of the date in UTC.
getUtcOffset ( $timezone ) : integer Returns the offset to UTC time for the given timezone
isEarlier ( Date $date ) : boolean Returns true if the current date is earlier than the given $date.
isLater ( Date $date ) : boolean Returns true if the current date is older than the given $date.
isLeapYear ( ) : boolean Returns true if the current year is a leap year, false otherwise.
isToday ( ) : boolean Returns true if current date is today.
now ( ) : Date Returns a date object set to now in UTC (same as {@link today()}, except that the time is also set).
secondsToDays ( integer $secs ) : float Returns the number of days represented by a number of seconds.
setDay ( integer $day ) : Date Returns a new Date instance with $this date's time of day and the day specified by $day.
setTime ( string $time ) : Date Returns a new Date instance with $this date's day and the specified new time of day.
setTimezone ( string $timezone ) : Date Returns a new date object with the same timestamp as $this but with a new timezone.
setYear ( integer $year ) : Date Returns a new Date instance with $this date's time of day, month and day, but with a new year (specified by $year).
subDay ( integer $n ) : Date Subtracts $n number of days from $this date and returns a new Date object.
subHour ( integer $n ) : Date Subtracts $n hours from $this date and returns the result in a new Date.
subMonth ( integer $n ) : Date Subtracts $n months from $this date and returns the result as a new Date object.
subPeriod ( integer $n, string $period ) : Date Subtracts a period from $this date and returns the result in a new Date instance.
subSeconds ( integer $n ) : Date Subtracts $n seconds from $this date and returns the result in a new Date.
subWeek ( integer $n ) : Date Subtracts $n weeks from $this date and returns a new Date object.
subYear ( integer $n ) : Date Subtracts $n years from $this date and returns the result as a new Date object.
toString ( string $format = 'Y-m-d' ) : string Converts this date to the requested string format. See {@link http://php.net/date} for the list of format strings.
today ( ) : Date Returns a date object set to today at midnight in UTC.
yesterday ( ) : Date Returns a date object set to yesterday at midnight in UTC.
yesterdaySameTime ( ) : Date Returns a date object set to yesterday with the current time of day in UTC.

Защищенные методы

Метод Описание
__construct ( integer $timestamp, string $timezone = 'UTC' ) Constructor.
extractUtcOffset ( string $timezone ) : integer | boolean Helper function that returns the offset in the timezone string 'UTC+14' Returns false if the timezone is not UTC+X or UTC-X
formatToken ( $token )
parseFormat ( string $pattern ) : array Parses the datetime format pattern and returns a tokenized result array

Приватные методы

Метод Описание
getInvalidDateFormatException ( $dateString )
getMaxDaysInMonth ( $timestamp )

Описание методов

__construct() защищенный Метод

Constructor.
protected __construct ( integer $timestamp, string $timezone = 'UTC' )
$timestamp integer The number in seconds since the unix epoch.
$timezone string The timezone of the datetime.

__toString() публичный Метод

See {@link toString()}.
public __toString ( ) : string
Результат string The current date in `'YYYY-MM-DD'` format.

addDay() публичный Метод

instance.
public addDay ( integer $n ) : Date
$n integer Number of days to add, must be > 0.
Результат Date

addHour() публичный Метод

Adds $n hours to $this date and returns the result in a new Date.
public addHour ( integer $n ) : Date
$n integer Number of hours to add. Can be less than 0.
Результат Date

addHourTo() публичный статический Метод

Adds N number of hours to a UNIX timestamp and returns the result. Using this static function instead of {@link addHour()} will be faster since a Date instance does not have to be created.
public static addHourTo ( integer $timestamp, number $n ) : integer
$timestamp integer The timestamp to add to.
$n number Number of hours to add, must be > 0.
Результат integer The result as a UNIX timestamp.

addPeriod() публичный Метод

Adds a period to $this date and returns the result in a new Date instance.
public addPeriod ( integer $n, string $period ) : Date
$n integer The number of periods to add. Can be negative.
$period string The type of period to add (YEAR, MONTH, WEEK, DAY, ...)
Результат Date

adjustForTimezone() публичный статический Метод

Converts a timestamp from UTC to a timezone.
public static adjustForTimezone ( integer $timestamp, string $timezone ) : integer
$timestamp integer The UNIX timestamp to adjust.
$timezone string The timezone to adjust to.
Результат integer The adjusted time as seconds from EPOCH.

compareMonth() публичный Метод

Performs three-way comparison of the month of the current date against the given $date's month.
public compareMonth ( Date $date ) : integer
$date Date Month to compare
Результат integer Returns `0` if the current month is equal to `$date`'s, `-1` if the current month is earlier or `1` if the current month is later.

compareWeek() публичный Метод

Performs three-way comparison of the week of the current date against the given $date's week.
public compareWeek ( Date $date ) : integer
$date Date
Результат integer Returns `0` if the current week is equal to `$date`'s, `-1` if the current week is earlier or `1` if the current week is later.

compareYear() публичный Метод

Performs three-way comparison of the month of the current date against the given $date's year.
public compareYear ( Date $date ) : integer
$date Date Year to compare
Результат integer Returns `0` if the current year is equal to `$date`'s, `-1` if the current year is earlier or `1` if the current year is later.

extractUtcOffset() защищенный статический Метод

Helper function that returns the offset in the timezone string 'UTC+14' Returns false if the timezone is not UTC+X or UTC-X
protected static extractUtcOffset ( string $timezone ) : integer | boolean
$timezone string
Результат integer | boolean utc offset or false

factory() публичный статический Метод

Creates a new Date instance using a string datetime value. The timezone of the Date result will be in UTC.
public static factory ( string | integer $dateString, string $timezone = null ) : Date
$dateString string | integer `'today'`, `'yesterday'`, `'now'`, `'yesterdaySameTime'`, a string with `'YYYY-MM-DD HH:MM:SS'` format or a unix timestamp.
$timezone string The timezone of the result. If specified, `$dateString` will be converted from UTC to this timezone before being used in the Date return value.
Результат Date

formatToken() защищенный Метод

protected formatToken ( $token )

getDateEndUTC() публичный Метод

Returns the end of the day of the current timestamp in UTC. For example, if the current timestamp is '2007-07-24 14:03:24' in UTC, the result will be '2007-07-24 23:59:59'.
public getDateEndUTC ( ) : string
Результат string

getDateStartUTC() публичный Метод

Returns the start of the day of the current timestamp in UTC. For example, if the current timestamp is '2007-07-24 14:04:24' in UTC, the result will be '2007-07-24'.
public getDateStartUTC ( ) : string
Результат string

getDatetime() публичный Метод

Returns the current timestamp as a string with the following format: 'YYYY-MM-DD HH:MM:SS'.
public getDatetime ( ) : string
Результат string

getDatetimeFromTimestamp() публичный статический Метод

Returns the date in the "Y-m-d H:i:s" PHP format
public static getDatetimeFromTimestamp ( integer $timestamp ) : string
$timestamp integer
Результат string

getHourUTC() публичный Метод

Returns the current hour in UTC timezone.
public getHourUTC ( ) : string
Результат string

getLocalized() публичный Метод

The template should contain tags that will be replaced with localized date strings.
public getLocalized ( string $template ) : string
$template string eg. `"MMM y"`
Результат string eg. `"Aug 2009"`

getTimestamp() публичный Метод

Returns the unix timestamp of the date in UTC, converted from the current timestamp timezone.
public getTimestamp ( ) : integer
Результат integer

getTimestampUTC() публичный Метод

Returns the Unix timestamp of the date in UTC.
public getTimestampUTC ( ) : integer
Результат integer

getUtcOffset() публичный статический Метод

Returns the offset to UTC time for the given timezone
public static getUtcOffset ( $timezone ) : integer
$timezone
Результат integer offest in minutes

isEarlier() публичный Метод

Returns true if the current date is earlier than the given $date.
public isEarlier ( Date $date ) : boolean
$date Date
Результат boolean

isLater() публичный Метод

Returns true if the current date is older than the given $date.
public isLater ( Date $date ) : boolean
$date Date
Результат boolean

isLeapYear() публичный Метод

Returns true if the current year is a leap year, false otherwise.
public isLeapYear ( ) : boolean
Результат boolean

isToday() публичный Метод

Returns true if current date is today.
public isToday ( ) : boolean
Результат boolean

now() публичный статический Метод

Returns a date object set to now in UTC (same as {@link today()}, except that the time is also set).
public static now ( ) : Date
Результат Date

parseFormat() защищенный статический Метод

Examples: Input Output 'dd.mm.yyyy' array(array('dd'), '.', array('mm'), '.', array('yyyy')) 'y?M?d?EEEE ah:mm:ss' array(array('y'), '?', array('M'), '?', array('d'), '?', array('EEEE'), ' ', array('a'), array('h'), ':', array('mm'), ':', array('ss'))
protected static parseFormat ( string $pattern ) : array
$pattern string the pattern to be parsed
Результат array tokenized parsing result

secondsToDays() публичный статический Метод

Returns the number of days represented by a number of seconds.
public static secondsToDays ( integer $secs ) : float
$secs integer
Результат float

setDay() публичный Метод

Returns a new Date instance with $this date's time of day and the day specified by $day.
public setDay ( integer $day ) : Date
$day integer The day eg. `31`.
Результат Date

setTime() публичный Метод

Returns a new Date instance with $this date's day and the specified new time of day.
public setTime ( string $time ) : Date
$time string String in the `'HH:MM:SS'` format.
Результат Date The new date with the time of day changed.

setTimezone() публичный Метод

See {@link getTimestamp()} to see how the timezone is used.
public setTimezone ( string $timezone ) : Date
$timezone string eg, `'UTC'`, `'Europe/London'`, etc.
Результат Date

setYear() публичный Метод

Returns a new Date instance with $this date's time of day, month and day, but with a new year (specified by $year).
public setYear ( integer $year ) : Date
$year integer The year, eg. `2010`.
Результат Date

subDay() публичный Метод

Subtracts $n number of days from $this date and returns a new Date object.
public subDay ( integer $n ) : Date
$n integer An integer > 0.
Результат Date

subHour() публичный Метод

Subtracts $n hours from $this date and returns the result in a new Date.
public subHour ( integer $n ) : Date
$n integer Number of hours to subtract. Can be less than 0.
Результат Date

subMonth() публичный Метод

Subtracts $n months from $this date and returns the result as a new Date object.
public subMonth ( integer $n ) : Date
$n integer An integer > 0.
Результат Date new date

subPeriod() публичный Метод

Subtracts a period from $this date and returns the result in a new Date instance.
public subPeriod ( integer $n, string $period ) : Date
$n integer The number of periods to add. Can be negative.
$period string The type of period to add (YEAR, MONTH, WEEK, DAY, ...)
Результат Date

subSeconds() публичный Метод

Subtracts $n seconds from $this date and returns the result in a new Date.
public subSeconds ( integer $n ) : Date
$n integer Number of seconds to subtract. Can be less than 0.
Результат Date

subWeek() публичный Метод

Subtracts $n weeks from $this date and returns a new Date object.
public subWeek ( integer $n ) : Date
$n integer An integer > 0.
Результат Date

subYear() публичный Метод

Subtracts $n years from $this date and returns the result as a new Date object.
public subYear ( integer $n ) : Date
$n integer An integer > 0.
Результат Date

toString() публичный Метод

Converts this date to the requested string format. See {@link http://php.net/date} for the list of format strings.
public toString ( string $format = 'Y-m-d' ) : string
$format string
Результат string

today() публичный статический Метод

Returns a date object set to today at midnight in UTC.
public static today ( ) : Date
Результат Date

yesterday() публичный статический Метод

Returns a date object set to yesterday at midnight in UTC.
public static yesterday ( ) : Date
Результат Date

yesterdaySameTime() публичный статический Метод

Returns a date object set to yesterday with the current time of day in UTC.
public static yesterdaySameTime ( ) : Date
Результат Date

Описание свойств

$timestamp защищенное свойство

The returned timestamp via getTimestamp() will have the conversion applied
protected int|null $timestamp
Результат integer | null

$timezone защищенное свойство

Timezone will only affect the returned timestamp via getTimestamp()
protected string $timezone
Результат string

$tokens защищенное статическое свойство

protected static $tokens