ICanBoogie/DateTime v1.2.0
  • Namespace
  • Class

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
Extended by ICanBoogie\DateTime implements JsonSerializable
Namespace: ICanBoogie
See: http://en.wikipedia.org/wiki/ISO_8601
Located at DateTime.php

Methods summary

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

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 ICanBoogie\DateTime
# now( )

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 ICanBoogie\DateTime
# right_now( )

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 ICanBoogie\DateTime
# none( DateTimeZone|string $timezone = 'utc' )

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 )
protected ICanBoogie\DateTime
# get_monday( )

Returns Monday of the week.

Returns Monday of the week.

Returns

ICanBoogie\DateTime
protected ICanBoogie\DateTime
# get_tuesday( )

Returns Tuesday of the week.

Returns Tuesday of the week.

Returns

ICanBoogie\DateTime
protected ICanBoogie\DateTime
# get_wednesday( )

Returns Wednesday of the week.

Returns Wednesday of the week.

Returns

ICanBoogie\DateTime
protected ICanBoogie\DateTime
# get_thursday( )

Returns Thursday of the week.

Returns Thursday of the week.

Returns

ICanBoogie\DateTime
protected ICanBoogie\DateTime
# get_friday( )

Returns Friday of the week.

Returns Friday of the week.

Returns

ICanBoogie\DateTime
protected ICanBoogie\DateTime
# get_saturday( )

Returns Saturday of the week.

Returns Saturday of the week.

Returns

ICanBoogie\DateTime
protected ICanBoogie\DateTime
# get_sunday( )

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 string
# __toString( )

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 string
# jsonSerialize( )

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 ICanBoogie\DateTime
# change( array $options, boolean $cascade = false )

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 ICanBoogie\DateTime
# with( array $options, boolean $cascade = false )

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 mixed
# localize( string $locale = 'en' )

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 string
# format_as_atom( )

format_as_atom() Formats the instance according to ATOM.

format_as_atom() Formats the instance according to ATOM.

Returns

string
public string
# format_as_cookie( )

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 string
# format_as_iso8601( )

format_as_iso8601() Formats the instance according to ISO8601.

format_as_iso8601() Formats the instance according to ISO8601.

Returns

string
public string
# format_as_rfc822( )

format_as_rfc822() Formats the instance according to RFC822.

format_as_rfc822() Formats the instance according to RFC822.

Returns

string
public string
# format_as_rfc850( )

format_as_rfc850() Formats the instance according to RFC850.

format_as_rfc850() Formats the instance according to RFC850.

Returns

string
public string
# format_as_rfc1036( )

format_as_rfc1036() Formats the instance according to RFC1036.

format_as_rfc1036() Formats the instance according to RFC1036.

Returns

string
public string
# format_as_rfc1123( )

format_as_rfc1123() Formats the instance according to RFC1123.

format_as_rfc1123() Formats the instance according to RFC1123.

Returns

string
public string
# format_as_rfc2822( )

format_as_rfc2822() Formats the instance according to RFC2822.

format_as_rfc2822() Formats the instance according to RFC2822.

Returns

string
public string
# format_as_rfc3339( )

format_as_rfc3339() Formats the instance according to RFC3339.

format_as_rfc3339() Formats the instance according to RFC3339.

Returns

string
public string
# format_as_rss( )

format_as_rss() Formats the instance according to RSS.

format_as_rss() Formats the instance according to RSS.

Returns

string
public string
# format_as_w3c( )

format_as_w3c() Formats the instance according to W3C.

format_as_w3c() Formats the instance according to W3C.

Returns

string
public string
# format_as_db( )

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 string
# format_as_number( )

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 string
# format_as_date( )

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 string
# format_as_time( )

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

string COOKIE

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'
string DB

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

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

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

Number (example: 20130203205903)

Number (example: 20130203205903)

# 'YmdHis'
string DATE

Date (example: 2013-02-03)

Date (example: 2013-02-03)

# 'Y-m-d'
string TIME

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 callable $localizer

Callable used to create localized instances.

Callable used to create localized instances.

# null

Magic properties

public integer $timestamp

Unix timestamp.

public integer $day

Day of the month.

public integer $hour

Hour of the day.

public integer $minute

Minute of the hour.

public integer $month

Month of the year.

public integer $second

Second of the minute.

public integer $year

Year.

public ICanBoogie\TimeZone $zone

The timezone of the instance.

public read-only integer $quarter

Quarter of the year.

public read-only integer $week

Week of the year.

public read-only integer $weekday

Day of the week.

public read-only integer $year_day

Day of the year.

public read-only boolean $is_monday

true if the instance represents Monday.

public read-only boolean $is_tuesday

true if the instance represents Tuesday.

public read-only boolean $is_wednesday

true if the instance represents Wednesday.

public read-only boolean $is_thursday

true if the instance represents Thursday.

public read-only boolean $is_friday

true if the instance represents Friday.

public read-only boolean $is_saturday

true if the instance represents Saturday.

public read-only boolean $is_sunday

true if the instance represents Sunday.

public read-only boolean $is_today

true if the instance is today.

public read-only boolean $is_past

true if the instance lies in the past.

public read-only boolean $is_future

true if the instance lies in the future.

public read-only boolean $is_empty

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

public read-only ICanBoogie\DateTime $tomorrow

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

public read-only ICanBoogie\DateTime $yesterday

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

public read-only ICanBoogie\DateTime $monday

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

public read-only ICanBoogie\DateTime $tuesday

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

public read-only ICanBoogie\DateTime $wednesday

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

public read-only ICanBoogie\DateTime $thursday

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

public read-only ICanBoogie\DateTime $friday

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

public read-only ICanBoogie\DateTime $saturday

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

public read-only ICanBoogie\DateTime $sunday

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

public read-only string $as_atom

The instance formatted according to ATOM.

public read-only string $as_cookie

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

public read-only string $as_iso8601

The instance formatted according to ISO8601.

public read-only string $as_rfc822

The instance formatted according to RFC822.

public read-only string $as_rfc850

The instance formatted according to RFC850.

public read-only string $as_rfc1036

The instance formatted according to RFC1036.

public read-only string $as_rfc1123

The instance formatted according to RFC1123.

public read-only string $as_rfc2822

The instance formatted according to RFC2822.

public read-only string $as_rfc3339

The instance formatted according to RFC3339.

public read-only string $as_rss

The instance formatted according to RSS.

public read-only string $as_w3c

The instance formatted according to W3C.

public read-only string $as_db

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

public read-only string $as_number

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

public read-only string $as_date

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

public read-only string $as_time

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

public read-only ICanBoogie\DateTime $utc

A new instance in the UTC timezone.

public read-only ICanBoogie\DateTime $local

A new instance in the local timezone.

public read-only boolean $is_utc

true if the instance is in the UTC timezone.

public read-only boolean $is_local

true if the instance is in the local timezone.

public read-only boolean $is_dst

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

ICanBoogie/DateTime v1.2.0 API documentation generated by ApiGen