class DateTime extends DateTime implements DateTimeInterface (View source)

An extension of PHP's DateTime class to provide dirty flagging and easier formatting options.

All date and datetime fields from your database will be created as instances of this class.

Example of formatting and changing the default format:

$now = new ActiveRecord\DateTime('2010-01-02 03:04:05'); ActiveRecord\DateTime::$DEFAULT_FORMAT = 'short';

echo $now->format(); # 02 Jan 03:04 echo $now->format('atom'); # 2010-01-02T03:04:05-05:00 echo $now->format('Y-m-d'); # 2010-01-02

__toString() uses the default formatter

echo (string)$now; # 02 Jan 03:04

You can also add your own pre-defined friendly formatters:

ActiveRecord\DateTime::$FORMATS['awesome_format'] = 'H:i:s m/d/Y'; echo $now->format('awesome_format') # 03:04:05 01/02/2010

Properties

static $DEFAULT_FORMAT Default format used for format() and __toString()
static $FORMATS Pre-defined format strings.

Methods

void
attribute_of(Model $model, string $attribute_name)

Indicates this object is an attribute of the specified model, with the given attribute name.

format($format = null)

Formats the DateTime to the specified format.

static string
get_format(string $format = null)

Returns the format string.

static 
createFromFormat($format, $time, $tz = null)

This needs to be overriden so it returns an instance of this class instead of PHP's \DateTime.

__toString()

No description

void
__clone()

Handle PHP object clone.

setDate($year, $month, $day)

No description

setISODate($year, $week, $day = 1)

No description

setTime($hour, $minute, $second, $microseconds)

No description

setTimestamp($unixtimestamp)

No description

setTimezone($timezone)

No description

modify($modify)

No description

add($interval)

No description

sub($interval)

No description

Details

void attribute_of(Model $model, string $attribute_name)

Indicates this object is an attribute of the specified model, with the given attribute name.

Parameters

Model $model The model this object is an attribute of
string $attribute_name The attribute name

Return Value

void

format($format = null)

Formats the DateTime to the specified format.

$datetime->format(); # uses the format defined in DateTime::$DEFAULT_FORMAT $datetime->format('short'); # d M H:i $datetime->format('Y-m-d'); # Y-m-d

Parameters

$format

See also

FORMATS
get_format

static string get_format(string $format = null)

Returns the format string.

If $format is a pre-defined format in $FORMATS it will return that otherwise it will assume $format is a format string itself.

Parameters

string $format A pre-defined string format or a raw format string

Return Value

string a format string

See also

FORMATS

static createFromFormat($format, $time, $tz = null)

This needs to be overriden so it returns an instance of this class instead of PHP's \DateTime.

See http://php.net/manual/en/datetime.createfromformat.php

Parameters

$format
$time
$tz

__toString()

void __clone()

Handle PHP object clone.

This makes sure that the object doesn't still flag an attached model as dirty after cloning the DateTime object and making modifications to it.

Return Value

void

setDate($year, $month, $day)

Parameters

$year
$month
$day

setISODate($year, $week, $day = 1)

Parameters

$year
$week
$day

setTime($hour, $minute, $second, $microseconds)

Parameters

$hour
$minute
$second
$microseconds

setTimestamp($unixtimestamp)

Parameters

$unixtimestamp

setTimezone($timezone)

Parameters

$timezone

modify($modify)

Parameters

$modify

add($interval)

Parameters

$interval

sub($interval)

Parameters

$interval