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 $time = new DateTime('0000-00-00', 'utc'); // or $time = DateTime::none(); echo $time; // -0001-11-30T00:00:00Z echo $time->is_empty; // true echo $time->as_date; // 0000-00-00 echo $time->as_db; // 0000-00-00 00:00:00
- DateTime implements DateTimeInterface
- ┗ ICanBoogie\DateTime implements JsonSerializable
Methods summary
public static
from( mixed $source
, mixed $timezone
= null )
: ICanBoogie\DateTime
Creates a ICanBoogie\DateTime
instance from a source.
public static
now( void )
: ICanBoogie\DateTime
Returns an instance with the current local time and the local time zone.
public static
none( DateTimeZone|string $timezone
= 'utc' )
: ICanBoogie\DateTime
Returns an instance representing an empty date ("0000-00-00").
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.
public
__set( $property
, $value
)
Sets the $year, $month, $day, $hour, $minute, $second, $timestamp and $zone properties.
public
change( array $options
, boolean $cascade
= false )
: ICanBoogie\DateTime
Modifies the properties of the instance according to the options.
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".
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_iso8601( void )
: string
format_as_iso8601() Formats the instance according to ISO8601.
public
format_as_rfc822( void )
: string
format_as_rfc822() Formats the instance according to RFC822.
public
format_as_rfc850( void )
: string
format_as_rfc850() Formats the instance according to RFC850.
public
format_as_rfc1036( void )
: string
format_as_rfc1036() Formats the instance according to RFC1036.
public
format_as_rfc1123( void )
: string
format_as_rfc1123() Formats the instance according to RFC1123.
public
format_as_rfc2822( void )
: string
format_as_rfc2822() Formats the instance according to RFC2822.
public
format_as_rfc3339( void )
: string
format_as_rfc3339() Formats the instance according to RFC3339.
public
format_as_db( void )
: string
format_as_db() Formats the instance according to ICanBoogie\DateTime::DB
.
public
format_as_number( void )
: string
format_as_number() Formats the instance according to ICanBoogie\DateTime::NUMBER
.
public
format_as_date( void )
: string
format_as_date() Formats the instance according to ICanBoogie\DateTime::DATE
.
public
format_as_time( void )
: string
format_as_time() Formats the instance according to ICanBoogie\DateTime::TIME
.
Constants summary
Constants inherited from DateTime
ATOM
,
COOKIE
,
ISO8601
,
RFC1036
,
RFC1123
,
RFC2822
,
RFC3339
,
RFC3339_EXTENDED
,
RFC822
,
RFC850
,
RSS
,
W3C
Properties summary
Magic properties
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_number
: string
The instance formatted according to ICanBoogie\DateTime::NUMBER
.