PHP Class 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");
Afficher le fichier Open project: piwik/piwik Class Usage Examples

Protected Properties

Свойство Type Description
$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

Méthodes publiques

Méthode Description
__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.

Méthodes protégées

Méthode Description
__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

Private Methods

Méthode Description
getInvalidDateFormatException ( $dateString )
getMaxDaysInMonth ( $timestamp )

Method Details

__construct() protected méthode

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() public méthode

See {@link toString()}.
public __toString ( ) : string
Résultat string The current date in `'YYYY-MM-DD'` format.

addDay() public méthode

instance.
public addDay ( integer $n ) : Date
$n integer Number of days to add, must be > 0.
Résultat Date

addHour() public méthode

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.
Résultat Date

addHourTo() public static méthode

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.
Résultat integer The result as a UNIX timestamp.

addPeriod() public méthode

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, ...)
Résultat Date

adjustForTimezone() public static méthode

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.
Résultat integer The adjusted time as seconds from EPOCH.

compareMonth() public méthode

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
Résultat 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() public méthode

Performs three-way comparison of the week of the current date against the given $date's week.
public compareWeek ( Date $date ) : integer
$date Date
Résultat 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() public méthode

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
Résultat 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() protected static méthode

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
Résultat integer | boolean utc offset or false

factory() public static méthode

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.
Résultat Date

formatToken() protected méthode

protected formatToken ( $token )

getDateEndUTC() public méthode

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
Résultat string

getDateStartUTC() public méthode

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
Résultat string

getDatetime() public méthode

Returns the current timestamp as a string with the following format: 'YYYY-MM-DD HH:MM:SS'.
public getDatetime ( ) : string
Résultat string

getDatetimeFromTimestamp() public static méthode

Returns the date in the "Y-m-d H:i:s" PHP format
public static getDatetimeFromTimestamp ( integer $timestamp ) : string
$timestamp integer
Résultat string

getHourUTC() public méthode

Returns the current hour in UTC timezone.
public getHourUTC ( ) : string
Résultat string

getLocalized() public méthode

The template should contain tags that will be replaced with localized date strings.
public getLocalized ( string $template ) : string
$template string eg. `"MMM y"`
Résultat string eg. `"Aug 2009"`

getTimestamp() public méthode

Returns the unix timestamp of the date in UTC, converted from the current timestamp timezone.
public getTimestamp ( ) : integer
Résultat integer

getTimestampUTC() public méthode

Returns the Unix timestamp of the date in UTC.
public getTimestampUTC ( ) : integer
Résultat integer

getUtcOffset() public static méthode

Returns the offset to UTC time for the given timezone
public static getUtcOffset ( $timezone ) : integer
$timezone
Résultat integer offest in minutes

isEarlier() public méthode

Returns true if the current date is earlier than the given $date.
public isEarlier ( Date $date ) : boolean
$date Date
Résultat boolean

isLater() public méthode

Returns true if the current date is older than the given $date.
public isLater ( Date $date ) : boolean
$date Date
Résultat boolean

isLeapYear() public méthode

Returns true if the current year is a leap year, false otherwise.
public isLeapYear ( ) : boolean
Résultat boolean

isToday() public méthode

Returns true if current date is today.
public isToday ( ) : boolean
Résultat boolean

now() public static méthode

Returns a date object set to now in UTC (same as {@link today()}, except that the time is also set).
public static now ( ) : Date
Résultat Date

parseFormat() protected static méthode

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
Résultat array tokenized parsing result

secondsToDays() public static méthode

Returns the number of days represented by a number of seconds.
public static secondsToDays ( integer $secs ) : float
$secs integer
Résultat float

setDay() public méthode

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`.
Résultat Date

setTime() public méthode

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.
Résultat Date The new date with the time of day changed.

setTimezone() public méthode

See {@link getTimestamp()} to see how the timezone is used.
public setTimezone ( string $timezone ) : Date
$timezone string eg, `'UTC'`, `'Europe/London'`, etc.
Résultat Date

setYear() public méthode

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`.
Résultat Date

subDay() public méthode

Subtracts $n number of days from $this date and returns a new Date object.
public subDay ( integer $n ) : Date
$n integer An integer > 0.
Résultat Date

subHour() public méthode

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.
Résultat Date

subMonth() public méthode

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.
Résultat Date new date

subPeriod() public méthode

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, ...)
Résultat Date

subSeconds() public méthode

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.
Résultat Date

subWeek() public méthode

Subtracts $n weeks from $this date and returns a new Date object.
public subWeek ( integer $n ) : Date
$n integer An integer > 0.
Résultat Date

subYear() public méthode

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.
Résultat Date

toString() public méthode

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
Résultat string

today() public static méthode

Returns a date object set to today at midnight in UTC.
public static today ( ) : Date
Résultat Date

yesterday() public static méthode

Returns a date object set to yesterday at midnight in UTC.
public static yesterday ( ) : Date
Résultat Date

yesterdaySameTime() public static méthode

Returns a date object set to yesterday with the current time of day in UTC.
public static yesterdaySameTime ( ) : Date
Résultat Date

Property Details

$timestamp protected_oe property

The returned timestamp via getTimestamp() will have the conversion applied
protected int|null $timestamp
Résultat integer | null

$timezone protected_oe property

Timezone will only affect the returned timestamp via getTimestamp()
protected string $timezone
Résultat string

$tokens protected_oe static_oe property

protected static $tokens