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");
Mostra file Open project: piwik/piwik Class Usage Examples

Protected Properties

Property 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

Public Methods

Method 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.

Protected Methods

Method 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

Method Description
getInvalidDateFormatException ( $dateString )
getMaxDaysInMonth ( $timestamp )

Method Details

__construct() protected method

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 method

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

addDay() public method

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

addHour() public method

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.
return Date

addHourTo() public static method

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

addPeriod() public method

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, ...)
return Date

adjustForTimezone() public static method

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

compareMonth() public method

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
return 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 method

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

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
return 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 method

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
return integer | boolean utc offset or false

factory() public static method

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.
return Date

formatToken() protected method

protected formatToken ( $token )

getDateEndUTC() public method

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
return string

getDateStartUTC() public method

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
return string

getDatetime() public method

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

getDatetimeFromTimestamp() public static method

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

getHourUTC() public method

Returns the current hour in UTC timezone.
public getHourUTC ( ) : string
return string

getLocalized() public method

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

getTimestamp() public method

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

getTimestampUTC() public method

Returns the Unix timestamp of the date in UTC.
public getTimestampUTC ( ) : integer
return integer

getUtcOffset() public static method

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

isEarlier() public method

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

isLater() public method

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

isLeapYear() public method

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

isToday() public method

Returns true if current date is today.
public isToday ( ) : boolean
return boolean

now() public static method

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

parseFormat() protected static method

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
return array tokenized parsing result

secondsToDays() public static method

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

setDay() public method

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`.
return Date

setTime() public method

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

setTimezone() public method

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

setYear() public method

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`.
return Date

subDay() public method

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

subHour() public method

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.
return Date

subMonth() public method

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.
return Date new date

subPeriod() public method

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, ...)
return Date

subSeconds() public method

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.
return Date

subWeek() public method

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

subYear() public method

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.
return Date

toString() public method

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
return string

today() public static method

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

yesterday() public static method

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

yesterdaySameTime() public static method

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

Property Details

$timestamp protected_oe property

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

$timezone protected_oe property

Timezone will only affect the returned timestamp via getTimestamp()
protected string $timezone
return string

$tokens protected_oe static_oe property

protected static $tokens