\Hazaar
Date
The date/time class

This class can be used to manage date and time values and perform date arithmetic.

Example

$date = new Hazaar\Date(‘next tuesday’); echo $date; //Echo’s a timestamp such as ’2013-01-15 11:00:00.0’

Timezones

It is possible to set the timezone globally for all date/time functions and classes.

For example, to set your timezone to ‘Australia/ACT’, in your application.ini file add the line:

php.date.timezone = Australia/ACT

See the PHP Manual for a list of valid timezones.

If a timezone is not set in the application.ini file, nor is one set in the global PHP configuration then as a last ditch effort the Date class will default to UTC. This is because not having an ini setting in date.timzone will cause a PHP runtime error.

Extends
\Datetime
Summary
Methods Properties Constants
add
age
day
end
sec
sub
No constants
Properties
Static
$calendar
$calendar
Static
$date_format
$date_format
Global format for date part
Static
$default_format
$default_format
$instance_format
$instance_format
Static
$time_format
$time_format
Global format for time part
$usec
$usec
Methods
__construct()
__construct($datetime = null, $timezone = null)

The Date class constructor takes two values. A datetime declaration and a timezone. Both are optional. If now datetime is specified then the current datetime is used. If no timezone is specified then the default timezone will be used.

Parameters

$datetimemixed

The datetime value you want to work with. This can be either an integer representing the datetime Epoch value (seconds since 1970-01-01) or a string datetime description supported by the PHP strtotime() function. This means that textual datetime descriptions such as ‘now’ and ‘next monday’ will work. See the PHP documentation on strtotime() for more information on valid formats. If the datetime value is not set, the current date and time will be used.

$timezonestring

The timezone for this datetime value. If no timezone is specified then the default timezone is used.

__export()
__export()
__tostring()
__tostring() : string
Magic method to output the datetime value as a string.

Uses the Date::timestamp() method.

Returns

string

String representation of the datetime value.

add()
add($interval, $return_new = false) : Date
Add a date/time interval to the current date/time.

See the PHP documentation on how to use the DateInterval object.

Parameters

$intervalmixed

Can be either a \DateInterval object or a string representing an interval, such as P1H to specify 1 hour.

$return_new\Hazaar\bool

Doesn’t update the current \Hazaar\Date object and instead returns a new object with the interval applied.

Returns

\Hazaar\Date
age()
age($precision = 0) : integer
Returns the age in years of the date with optional precision

Parameters

$precisioninteger

The number of digits to round an age to. Default: 0

Returns

integer

The number of years passed since the date value.

compare()
compare($date, $include_time = false)
Compare the current date with the argument

Parameters

$date

No description

$include_time

No description

date()
date() : string
Return the date part formatted by the global date format.

Returns

string

The date part of the datetime value

datetime()
datetime() : string
Return the date and time.

This will use the global date and time formats and concatenate them together, separated by a space.

Returns

string

The date and time of the datetime value

day()
day()
Returns the objects current day
daysInMonth()
daysInMonth() : int
Returns the number of days in the current month.

Returns

int
diff()
diff($timestamp, $return_seconds = false) : \DateInterval|int
Returns the difference between the current date/time value and the value supplied as the first argument.

Normally this method will return a \DateInterval object which is the default for the \DateTime class. However this functionality has been extended with the $return_seconds parameter which will instead return an integer value indicating the difference in whole seconds.

Parameters

$timestamp\DateTime

The timestamp to compare the current date/time to.

$return_seconds\Hazaar\bool

If TRUE then the return value will be an integer indicating the difference in whole seconds.

Returns

\DateInterval|int
end()
end() : Date
Returns the end time of the current date as a Date object.
firstOfMonth()
firstOfMonth() : Date
Returns the first day of the current month as a Date object.
firstOfWeek()
firstOfWeek() : Date
Returns the first day of the current week as a Date object.
firstOfYear()
firstOfYear() : Date
Returns the first day of the current year as a Date object.
formatTZ()
formatTZ($format, $timezone = 'UTC') : string
Return the date time value using a format string for a specified timezone

Parameters

$formatstring

A standard date format string

$timezonestring

The timezone to use to convert the date time value.

Returns

string

The date time string in the target timezone and format.

fuzzy()
fuzzy($precise = false) : string
Return a fuzzy diff between the current time and the Date value.

Parameters

$precise\Hazaar\bool

No description

Returns

string
getSQLDate()
getSQLDate() : string
Get the current datetime value as an SQL compliant formatted string.

Returns

string

The SQL compliant datetime string.

hour()
hour()
Returns the objects current hour(
lastOfMonth()
lastOfMonth() : Date
Returns the last day of the current month as a Date object.
lastOfWeek()
lastOfWeek() : Date
Returns the last day of the current week as a Date object.
lastOfYear()
lastOfYear() : Date
Returns the last day of the current year as a Date object.
minute()
minute()
Returns the objects current minute
month()
month()
Returns the objects current month
sec()
sec() : int
Returns the current datetime value as epoch (seconds passed since 1970-01-01).

Returns

int

Epoch value of datetime.

second()
second()
Returns the objects current second
setFormat()
setFormat($format)

Parameters

$format

No description

setTimezone()
setTimezone($timezone = null) : boolean
Set the timezone for the date object

Setting this will cause the date and time to be output using the specified timezone. If a timezone is not given then the default system timezone will be used. This is a good way to convert a date of another timezone into the current default system timezone.

Parameters

$timezoneMixed

The timezone to set. This can be either a DateTimeZone object, a string representation of a timezone, an offset in the format hh:mm, or a numeric value of the timezone returned by timezone_identifiers_list(). If it is left null, then the default system timezone is used.

Returns

boolean

Returns the result of the parent DateTime::setTimezone call.

start()
start() : Date
Returns the start time of the current date as a Date object.
sub()
sub($interval, $return_new = false) : Date
Subtract a date/time interval from the current date/time.

See the PHP documentation on how to use the DateInterval object.

Parameters

$intervalmixed

Can be either a \DateInterval object or a string representing an interval, such as P1H to specify 1 hour.

$return_new\Hazaar\bool

Doesn’t update the current \Hazaar\Date object and instead returns a new object with the interval applied.

Returns

\Hazaar\Date
time()
time() : string
Return the time part formatted by the global time format.

Returns

string

The time part of the datetime value

timestamp()
timestamp() : string
Get the timestamp formatted as a string.

This will use the global date and time formats and concatenate them together, separated by a space.

Returns

string

The timestamp as string

usec()
usec() : int
Return the microsecond part of the datetime value.

This will more than likely always be 0 (zero) unless specified in the constructor.

Returns

int

Microsecond part of datetime value.

year()
year()
Returns the objects current year