ICanBoogie
  • Documentation
  • API Reference
  • DateTime master
Namespaces
  • ICanBoogie
Classes
  • DateTime
  • TimeZone
  • TimeZoneLocation

Class DateTime

Representation of a date and time.

<?php

// Let's say that _now_ is 2013-02-03 21:03:45 in Paris

use ICanBoogie\DateTime;

date_default_timezone_set('EST'); // set local time zone to Eastern Standard Time

$time = new DateTime('now', 'Europe/Paris');

echo $time;                             // 2013-02-03T21:03:45+0100
echo $time->utc;                        // 2013-02-03T20:03:45Z
echo $time->local;                      // 2013-02-03T15:03:45-0500
echo $time->utc->local;                 // 2013-02-03T15:03:45-0500
echo $time->utc->is_utc;                // true
echo $time->utc->is_local;              // false
echo $time->local->is_utc;              // false
echo $time->local->is_local;            // true
echo $time->is_dst;                     // false

echo $time->as_rss;                     // Sun, 03 Feb 2013 21:03:45 +0100
echo $time->as_db;                      // 2013-02-03 21:03:45

echo $time->as_time;                    // 21:03:45
echo $time->utc->as_time;               // 20:03:45
echo $time->local->as_time;             // 15:03:45
echo $time->utc->local->as_time;        // 15:03:45

echo $time->quarter;                    // 1
echo $time->week;                       // 5
echo $time->day;                        // 3
echo $time->minute;                     // 3
echo $time->is_monday;                  // false
echo $time->is_saturday;                // true
echo $time->is_today;                   // true
echo $time->tomorrow;                   // 2013-02-04T00:00:00+0100
echo $time->tomorrow->is_future         // true
echo $time->yesterday;                  // 2013-02-02T00:00:00+0100
echo $time->yesterday->is_past          // true
echo $time->monday;                     // 2013-01-28T00:00:00+0100
echo $time->sunday;                     // 2013-02-03T00:00:00+0100

echo $time->timestamp;                  // 1359921825
echo $time;                             // 2013-02-03T21:03:45+0100
$time->timestamp += 3600 * 4;
echo $time;                             // 2013-02-04T01:03:45+0100

echo $time->zone;                       // Europe/Paris
echo $time->zone->offset;               // 3600
echo $time->zone->location;             // FR,48.86667,2.33333
echo $time->zone->location->latitude;   // 48.86667
$time->zone = 'Asia/Tokyo';
echo $time;                             // 2013-02-04T09:03:45+0900

$time->hour += 72;
echo "Rendez-vous in 72 hours: $time";  // Rendez-vous in 72 hours: 2013-02-07T05:03:45+0900

Empty dates are also supported:

<?php

use ICanBoogie\DateTime;

$time = new DateTime('0000-00-00', 'utc');
// or
$time = DateTime::none();

echo $time->is_empty;                   // true
echo $time->as_date;                    // 0000-00-00
echo $time->as_db;                      // 0000-00-00 00:00:00
echo $time;                             // ""
DateTime implements DateTimeInterface
┗ ICanBoogie\DateTime implements JsonSerializable
Namespace: ICanBoogie
See: http://en.wikipedia.org/wiki/ISO_8601
Located at DateTime.php

Methods summary

public static from( mixed $source, mixed $timezone = null ) : ICanBoogie\DateTime

Creates a ICanBoogie\DateTime instance from a source.

Creates a ICanBoogie\DateTime instance from a source.

<?php

use ICanBoogie\DateTime;

DateTime::from(new \DateTime('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris')));
DateTime::from('2001-01-01 01:01:01', 'Europe/Paris');
DateTime::from('now');

Parameters

$source
$timezone

The time zone to use to create the time. The value is ignored if the source is an instance of \DateTime.

Returns

ICanBoogie\DateTime
public static now( void ) : ICanBoogie\DateTime

Returns an instance with the current local time and the local time zone.

Returns an instance with the current local time and the local time zone.

Note: Subsequent calls return equal times, event if they are minutes apart. now actually refers to the REQUEST_TIME or, if it is now available, to the first time the method was invoked.

Returns

ICanBoogie\DateTime
public static right_now( void ) : ICanBoogie\DateTime

Returns an instance with the current local time and the local time zone.

Returns an instance with the current local time and the local time zone.

Note: Subsequent calls may return different times.

Returns

ICanBoogie\DateTime
public static none( DateTimeZone|string $timezone = 'utc' ) : ICanBoogie\DateTime

Returns an instance representing an empty date ("0000-00-00").

Returns an instance representing an empty date ("0000-00-00").

<?php

use ICanBoogie\DateTime;

$d = DateTime::none();
$d->is_empty;                      // true
$d->zone->name;                    // "UTC"

$d = DateTime::none('Asia/Tokyo');
$d->is_empty;                      // true
$d->zone->name;                    // "Asia/Tokio"

Parameters

$timezone

The time zone in which the empty date is created. Defaults to "UTC".

Returns

ICanBoogie\DateTime
public __construct( string $time = 'now', DateTimeZone|string|null $timezone = null )

If the time zone is specified as a string a \DateTimeZone instance is created and used instead.

If the time zone is specified as a string a \DateTimeZone instance is created and used instead.

<?php

use ICanBoogie\DateTime;

new DateTime('2001-01-01 01:01:01', new \DateTimeZone('Europe/Paris')));
new DateTime('2001-01-01 01:01:01', 'Europe/Paris');
new DateTime;

Parameters

$time
Defaults to "now".
$timezone

Overrides

DateTime::__construct
public __get( $property )

Inheritdoc

protected get_monday( void ) : ICanBoogie\DateTime

Returns Monday of the week.

Returns Monday of the week.

Returns

ICanBoogie\DateTime
protected get_tuesday( void ) : ICanBoogie\DateTime

Returns Tuesday of the week.

Returns Tuesday of the week.

Returns

ICanBoogie\DateTime
protected get_wednesday( void ) : ICanBoogie\DateTime

Returns Wednesday of the week.

Returns Wednesday of the week.

Returns

ICanBoogie\DateTime
protected get_thursday( void ) : ICanBoogie\DateTime

Returns Thursday of the week.

Returns Thursday of the week.

Returns

ICanBoogie\DateTime
protected get_friday( void ) : ICanBoogie\DateTime

Returns Friday of the week.

Returns Friday of the week.

Returns

ICanBoogie\DateTime
protected get_saturday( void ) : ICanBoogie\DateTime

Returns Saturday of the week.

Returns Saturday of the week.

Returns

ICanBoogie\DateTime
protected get_sunday( void ) : ICanBoogie\DateTime

Returns Sunday of the week.

Returns Sunday of the week.

Returns

ICanBoogie\DateTime
public __set( $property, $value )

Sets the $year, $month, $day, $hour, $minute, $second, $timestamp and $zone properties.

Sets the $year, $month, $day, $hour, $minute, $second, $timestamp and $zone properties.

Throws

PropertyNotWritable
in attempt to set a read-only property.
PropertyNotDefined
in attempt to set an unsupported property.

Inheritdoc

public __call( $method, $arguments )

Handles the format_as_* methods.

Handles the format_as_* methods.

If the format is RFC822 or RFC1123 and the time zone is equivalent to GMT, the offset +0000 is replaced by GMT according to the specs.

If the format is ISO8601 and the time zone is equivalent to UTC, the offset +0000 is replaced by Z according to the specs.

Throws

BadMethodCallException
in attempt to call an unsupported method.

Inheritdoc

public __toString( void ) : string

Returns the datetime formatted as ISO8601.

Returns the datetime formatted as ISO8601.

Returns

string

The instance rendered as an ISO8601 string, or an empty string if the datetime is empty.

public jsonSerialize( void ) : string

Returns a ISO8601 representation of the instance.

Returns a ISO8601 representation of the instance.

Returns

string

Implementation of

JsonSerializable::jsonSerialize()
public setTimezone( $timezone )

The timezone can be specified as a string.

The timezone can be specified as a string.

If the timezone is local the timezone returned by date_default_timezone_get() is used instead.

Inheritdoc

Overrides

DateTime::setTimezone
public change( array $options, boolean $cascade = false ) : ICanBoogie\DateTime

Modifies the properties of the instance according to the options.

Modifies the properties of the instance according to the options.

The following properties can be updated: $year, $month, $day, $hour, $minute and $second.

Note: Values exceeding ranges are added to their parent values.

<?php

use ICanBoogie\DateTime;

$time = new DateTime('now');
$time->change([ 'year' => 2000, 'second' => 0 ]);

Parameters

$options
$cascade

If true, time options (hour, minute, second) reset cascading, so if only the hour is passed, then minute and second is set to 0. If the hour and minute is passed, then second is set to 0.

Returns

ICanBoogie\DateTime
public with( array $options, boolean $cascade = false ) : ICanBoogie\DateTime

Instantiate a new instance with changes properties.

Instantiate a new instance with changes properties.

Parameters

$options
$cascade

Returns

ICanBoogie\DateTime
public format( $format )

If the instance represents an empty date and the format is ICanBoogie\DateTime::DATE or ICanBoogie\DateTime::DB, an empty date is returned, respectively "0000-00-00" and "0000-00-00 00:00:00". Note that the time information is discarded for ICanBoogie\DateTime::DB. This only apply to ICanBoogie\DateTime::DATE and ICanBoogie\DateTime::DB formats. For instance RSS will return the following string: "Wed, 30 Nov -0001 00:00:00 +0000".

If the instance represents an empty date and the format is ICanBoogie\DateTime::DATE or ICanBoogie\DateTime::DB, an empty date is returned, respectively "0000-00-00" and "0000-00-00 00:00:00". Note that the time information is discarded for ICanBoogie\DateTime::DB. This only apply to ICanBoogie\DateTime::DATE and ICanBoogie\DateTime::DB formats. For instance RSS will return the following string: "Wed, 30 Nov -0001 00:00:00 +0000".

Inheritdoc

Overrides

DateTime::format
public localize( string $locale = 'en' ) : mixed

Returns a localized instance.

Returns a localized instance.

Parameters

$locale

Returns

mixed

Throws

RuntimeException
if ICanBoogie\DateTime::$localizer is not defined.

Methods inherited from DateTime

__set_state(), __wakeup(), add(), createFromFormat(), diff(), getLastErrors(), getOffset(), getTimestamp(), getTimezone(), modify(), setDate(), setISODate(), setTime(), setTimestamp(), sub()

Magic methods summary

public format_as_atom( void ) : string

format_as_atom() Formats the instance according to ATOM.

format_as_atom() Formats the instance according to ATOM.

Returns

string
public format_as_cookie( void ) : string

format_as_cookie() Formats the instance according to ICanBoogie\DateTime::COOKIE.

format_as_cookie() Formats the instance according to ICanBoogie\DateTime::COOKIE.

Returns

string
public format_as_iso8601( void ) : string

format_as_iso8601() Formats the instance according to ISO8601.

format_as_iso8601() Formats the instance according to ISO8601.

Returns

string
public format_as_rfc822( void ) : string

format_as_rfc822() Formats the instance according to RFC822.

format_as_rfc822() Formats the instance according to RFC822.

Returns

string
public format_as_rfc850( void ) : string

format_as_rfc850() Formats the instance according to RFC850.

format_as_rfc850() Formats the instance according to RFC850.

Returns

string
public format_as_rfc1036( void ) : string

format_as_rfc1036() Formats the instance according to RFC1036.

format_as_rfc1036() Formats the instance according to RFC1036.

Returns

string
public format_as_rfc1123( void ) : string

format_as_rfc1123() Formats the instance according to RFC1123.

format_as_rfc1123() Formats the instance according to RFC1123.

Returns

string
public format_as_rfc2822( void ) : string

format_as_rfc2822() Formats the instance according to RFC2822.

format_as_rfc2822() Formats the instance according to RFC2822.

Returns

string
public format_as_rfc3339( void ) : string

format_as_rfc3339() Formats the instance according to RFC3339.

format_as_rfc3339() Formats the instance according to RFC3339.

Returns

string
public format_as_rss( void ) : string

format_as_rss() Formats the instance according to RSS.

format_as_rss() Formats the instance according to RSS.

Returns

string
public format_as_w3c( void ) : string

format_as_w3c() Formats the instance according to W3C.

format_as_w3c() Formats the instance according to W3C.

Returns

string
public format_as_db( void ) : string

format_as_db() Formats the instance according to ICanBoogie\DateTime::DB.

format_as_db() Formats the instance according to ICanBoogie\DateTime::DB.

Returns

string
public format_as_number( void ) : string

format_as_number() Formats the instance according to ICanBoogie\DateTime::NUMBER.

format_as_number() Formats the instance according to ICanBoogie\DateTime::NUMBER.

Returns

string
public format_as_date( void ) : string

format_as_date() Formats the instance according to ICanBoogie\DateTime::DATE.

format_as_date() Formats the instance according to ICanBoogie\DateTime::DATE.

Returns

string
public format_as_time( void ) : string

format_as_time() Formats the instance according to ICanBoogie\DateTime::TIME.

format_as_time() Formats the instance according to ICanBoogie\DateTime::TIME.

Returns

string

Constants summary

COOKIE : string

We redefine the constant to make sure that the cookie uses a valid pattern.

We redefine the constant to make sure that the cookie uses a valid pattern.

See

http://grokbase.com/t/php/php-bugs/111xynxd6m/php-bug-bug-53879-new-datetime-createfromformat-fails-to-parse-cookie-expiration-date
'l, d-M-Y H:i:s T'
DB : string

DB (example: 2013-02-03 20:59:03)

DB (example: 2013-02-03 20:59:03)

'Y-m-d H:i:s'
NUMBER : string

Number (example: 20130203205903)

Number (example: 20130203205903)

'YmdHis'
DATE : string

Date (example: 2013-02-03)

Date (example: 2013-02-03)

'Y-m-d'
TIME : string

Time (example: 20:59:03)

Time (example: 20:59:03)

'H:i:s'

Constants inherited from DateTime

ATOM, COOKIE, ISO8601, RFC1036, RFC1123, RFC2822, RFC3339, RFC3339_EXTENDED, RFC822, RFC850, RSS, W3C

Properties summary

public static $localizer : callable

Callable used to create localized instances.

Callable used to create localized instances.

null

Magic properties

public $timestamp : integer

Unix timestamp.

public $day : integer

Day of the month.

public $hour : integer

Hour of the day.

public $minute : integer

Minute of the hour.

public $month : integer

Month of the year.

public $second : integer

Second of the minute.

public $year : integer

Year.

public $zone : ICanBoogie\TimeZone

The timezone of the instance.

public read-only $quarter : integer

Quarter of the year.

public read-only $week : integer

Week of the year.

public read-only $weekday : integer

Day of the week.

public read-only $year_day : integer

Day of the year.

public read-only $is_monday : boolean

true if the instance represents Monday.

public read-only $is_tuesday : boolean

true if the instance represents Tuesday.

public read-only $is_wednesday : boolean

true if the instance represents Wednesday.

public read-only $is_thursday : boolean

true if the instance represents Thursday.

public read-only $is_friday : boolean

true if the instance represents Friday.

public read-only $is_saturday : boolean

true if the instance represents Saturday.

public read-only $is_sunday : boolean

true if the instance represents Sunday.

public read-only $is_today : boolean

true if the instance is today.

public read-only $is_past : boolean

true if the instance lies in the past.

public read-only $is_future : boolean

true if the instance lies in the future.

public read-only $is_empty : boolean

true if the instance represents an empty date such as "0000-00-00" or "0000-00-00 00:00:00".

public read-only $tomorrow : ICanBoogie\DateTime

A new instance representing the next day. Time is reset to 00:00:00.

public read-only $yesterday : ICanBoogie\DateTime

A new instance representing the previous day. Time is reset to 00:00:00.

public read-only $monday : ICanBoogie\DateTime

A new instance representing Monday of the week. Time is reset to 00:00:00.

public read-only $tuesday : ICanBoogie\DateTime

A new instance representing Tuesday of the week. Time is reset to 00:00:00.

public read-only $wednesday : ICanBoogie\DateTime

A new instance representing Wednesday of the week. Time is reset to 00:00:00.

public read-only $thursday : ICanBoogie\DateTime

A new instance representing Thursday of the week. Time is reset to 00:00:00.

public read-only $friday : ICanBoogie\DateTime

A new instance representing Friday of the week. Time is reset to 00:00:00.

public read-only $saturday : ICanBoogie\DateTime

A new instance representing Saturday of the week. Time is reset to 00:00:00.

public read-only $sunday : ICanBoogie\DateTime

A new instance representing Sunday of the week. Time is reset to 00:00:00.

public read-only $as_atom : string

The instance formatted according to ATOM.

public read-only $as_cookie : string

The instance formatted according to ICanBoogie\DateTime::COOKIE.

public read-only $as_iso8601 : string

The instance formatted according to ISO8601.

public read-only $as_rfc822 : string

The instance formatted according to RFC822.

public read-only $as_rfc850 : string

The instance formatted according to RFC850.

public read-only $as_rfc1036 : string

The instance formatted according to RFC1036.

public read-only $as_rfc1123 : string

The instance formatted according to RFC1123.

public read-only $as_rfc2822 : string

The instance formatted according to RFC2822.

public read-only $as_rfc3339 : string

The instance formatted according to RFC3339.

public read-only $as_rss : string

The instance formatted according to RSS.

public read-only $as_w3c : string

The instance formatted according to W3C.

public read-only $as_db : string

The instance formatted according to ICanBoogie\DateTime::DB.

public read-only $as_number : string

The instance formatted according to ICanBoogie\DateTime::NUMBER.

public read-only $as_date : string

The instance formatted according to ICanBoogie\DateTime::DATE.

public read-only $as_time : string

The instance formatted according to ICanBoogie\DateTime::TIME.

public read-only $utc : ICanBoogie\DateTime

A new instance in the UTC timezone.

public read-only $local : ICanBoogie\DateTime

A new instance in the local timezone.

public read-only $is_utc : boolean

true if the instance is in the UTC timezone.

public read-only $is_local : boolean

true if the instance is in the local timezone.

public read-only $is_dst : boolean

true if time occurs during Daylight Saving Time in its time zone.

DateTime master – Check on GitHub – API documentation generated by ApiGen