PHP Class Jyxo\Time\Time

Internally uses a \DateTime object. Initialization is possible using almost any date/time format (unix timestamp, SQL form, ...). Requires the Gettext PHP extension or any other implementation of the _(string) translation function.
Author: Jaroslav Hanslík
Author: Jan Kolibač
Author: Roman Řáha
Author: Martin Šamšula
Author: Ondřej Nešpor
Inheritance: implements Serializable
Datei anzeigen Open project: jyxo/php Class Usage Examples

Public Methods

Method Description
__call ( string $method, array $args ) : mixed Calls a method directly on the internal \DateTime object.
__construct ( string | integer | Time | DateTime $time, string | DateTimeZone $timeZone = null ) Constructor.
__get ( string $name ) : mixed Returns date/time in the requested format.
__toString ( ) : string Returns date/time in the unix timestamp format.
createFromFormat ( string $format, string $time ) : self Creates an instance using a date/time definition in the given format.
format ( string $format, string | DateTimeZone $timeZone = null ) : string Returns date/time in the given format with months and days translated.
formatAsInterval ( boolean $useTense = true, string | DateTimeZone $timeZone = null ) : string Function for formatting time differences into human readable forms.
formatExtended ( string $dateFormat = 'j. F Y', string $timeFormat = 'G:i', string | DateTimeZone $timeZone = null ) : string Returns date/time in the form of:
get ( string | integer | Time | DateTime $time, string | DateTimeZone $timeZone = null ) : self Helper function for creating an instance with the given date/time.
getTimeZone ( ) : DateTimeZone Returns the actual time zone.
hasHappened ( ) : boolean Checks if the date/time already happened.
minus ( integer | string $interval ) : self Returns a new date/time object and adds with the given interval subtracted.
now ( ) : self Returns an instance with the current date/time.
plus ( integer | string $interval ) : self Returns a new date/time object and adds with the given interval added.
serialize ( ) : string Object serialization.
setTimeZone ( string | DateTimeZone $timeZone ) : self Sets a new time zone.
truncate ( string $unit ) : self Returns a new instance with date/time truncated to the given unit.
unserialize ( string $serialized ) Object deserialization.

Protected Methods

Method Description
createTimeZone ( string | DateTimeZone $definition ) : DateTimeZone Creates a \DateTimeZone object from a time zone definition
revertOriginalTimeZone ( ) : self Reverts the original time zone.
setTemporaryTimeZone ( string | DateTimeZone $timeZone ) Sets a time zone temporarily.

Method Details

__call() public method

Calls a method directly on the internal \DateTime object.
public __call ( string $method, array $args ) : mixed
$method string Method name
$args array Method arguments
return mixed

__construct() public method

Creates an instance and initializes the internal date/time representation.
public __construct ( string | integer | Time | DateTime $time, string | DateTimeZone $timeZone = null )
$time string | integer | Time | DateTime Date/time definition
$timeZone string | DateTimeZone Time zone definition

__get() public method

Returns date/time in the requested format.
public __get ( string $name ) : mixed
$name string Format name
return mixed

__toString() public method

Returns date/time in the unix timestamp format.
public __toString ( ) : string
return string Returns empty string if the stored date/time has no valid UT representation

createFromFormat() public static method

Creates an instance using a date/time definition in the given format.
public static createFromFormat ( string $format, string $time ) : self
$format string Date/time format
$time string Date/time definition
return self

createTimeZone() protected method

Creates a \DateTimeZone object from a time zone definition
protected createTimeZone ( string | DateTimeZone $definition ) : DateTimeZone
$definition string | DateTimeZone Time zone definition
return DateTimeZone

format() public method

Returns date/time in the given format with months and days translated.
public format ( string $format, string | DateTimeZone $timeZone = null ) : string
$format string Requested format
$timeZone string | DateTimeZone Result time zone definition
return string

formatAsInterval() public method

$t < 10 seconds = Now 10 seconds <= $t < 60 seconds 1 minute <= $t < 1 hour 1 hour <= $t < 24 hours 1 day <= $t < 7 days 1 week <= $t < 4 weeks 1 month <= $t < 12 months 1 year <= $t < n years
public formatAsInterval ( boolean $useTense = true, string | DateTimeZone $timeZone = null ) : string
$useTense boolean Defines if declension should be used
$timeZone string | DateTimeZone Result time zone definition
return string

formatExtended() public method

Today at 10:00 Yesterday at 10:00 Friday at 10:00 21. March 2009 at 10:00
public formatExtended ( string $dateFormat = 'j. F Y', string $timeFormat = 'G:i', string | DateTimeZone $timeZone = null ) : string
$dateFormat string Date format
$timeFormat string Time format
$timeZone string | DateTimeZone Result time zone definition
return string

get() public static method

Useful for one-time usage.
public static get ( string | integer | Time | DateTime $time, string | DateTimeZone $timeZone = null ) : self
$time string | integer | Time | DateTime Date/time definition
$timeZone string | DateTimeZone Time zone definition
return self

getTimeZone() public method

Returns the actual time zone.
public getTimeZone ( ) : DateTimeZone
return DateTimeZone

hasHappened() public method

Compares the internal date/time with the current local time of the appropriate time zone.
public hasHappened ( ) : boolean
return boolean

minus() public method

Returns a new date/time object and adds with the given interval subtracted.
public minus ( integer | string $interval ) : self
$interval integer | string Number of seconds or a string compatible with the strtotime() function
return self

now() public static method

Returns an instance with the current date/time.
public static now ( ) : self
return self

plus() public method

Returns a new date/time object and adds with the given interval added.
public plus ( integer | string $interval ) : self
$interval integer | string Number of seconds or a string compatible with the strtotime() function
return self

revertOriginalTimeZone() protected method

Reverts the original time zone.
protected revertOriginalTimeZone ( ) : self
return self

serialize() public method

Object serialization.
public serialize ( ) : string
return string

setTemporaryTimeZone() protected method

Sets a time zone temporarily.
protected setTemporaryTimeZone ( string | DateTimeZone $timeZone )
$timeZone string | DateTimeZone Temporary time zone definition

setTimeZone() public method

Sets a new time zone.
public setTimeZone ( string | DateTimeZone $timeZone ) : self
$timeZone string | DateTimeZone The new time zone
return self

truncate() public method

Returns a new instance with date/time truncated to the given unit.
public truncate ( string $unit ) : self
$unit string Unit to truncate the date/time to
return self

unserialize() public method

Object deserialization.
public unserialize ( string $serialized )
$serialized string Serialized data