Date
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:
pre. 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.
Properties
usec
public int $usec
dateFormat
Global format for date part.
public string $dateFormat
timeFormat
Global format for time part.
public string $timeFormat
defaultFormat
public string $defaultFormat
instanceFormat
private string $instanceFormat
Methods
__construct
public __construct({Array})
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
Parameter | Type | Description |
---|---|---|
$datetime | mixed | 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
[[http://au1.php.net/manual/en/function.strtotime.php|strtotime()]] for more information on valid
formats. If the datetime value is not set, the current date and time will be used. |
| $timezone
| string
| The timezone for this datetime value. If no timezone is specified then the default timezone is used. |
__toString
Magic method to output the datetime value as a string.
public __toString({Array})
Uses the Date::timestamp() method.
__export
public __export({Array})
setFormat
public setFormat({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$format | string |
setTimezone
Set the timezone for the date object.
public setTimezone({Array})
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
Parameter | Type | Description |
---|---|---|
$timezone | string | 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. |
formatTZ
Return the date time value using a format string for a specified timezone.
public formatTZ({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$format | string | A standard date format string |
$timezone | string | The timezone to use to convert the date time value |
getSQLDate
Get the current datetime value as an SQL compliant formatted string.
public getSQLDate({Array})
sec
Returns the current datetime value as epoch (seconds passed since 1970-01-01).
public sec({Array})
usec
Return the microsecond part of the datetime value.
public usec({Array})
This will more than likely always be 0 (zero) unless specified in the constructor.
date
Return the date part formatted by the global date format.
public date({Array})
time
Return the time part formatted by the global time format.
public time({Array})
datetime
Return the date and time.
public datetime({Array})
This will use the global date and time formats and concatenate them together, separated by a space.
timestamp
Get the timestamp formatted as a string.
public timestamp({Array})
This will use the global date and time formats and concatenate them together, separated by a space.
age
Returns the age in years of the date with optional precision.
public age({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$precision | int | The number of digits to round an age to. Default: 0 |
diffSeconds
Returns the difference between the current date/time value and the value supplied as the first argument.
public diffSeconds({Array})
Normally this method will return a \DateInterval object which is the default for the \DateTime class. However this functionality has been extended with the $returnSeconds parameter which will instead return an integer value indicating the difference in whole seconds.
Parameters
Parameter | Type | Description |
---|---|---|
$timestamp | \DateTimeInterface | The timestamp to compare the current date/time to |
year
Returns the objects current year.
public year({Array})
month
Returns the objects current month.
public month({Array})
day
Returns the objects current day.
public day({Array})
hour
Returns the objects current hour(.
public hour({Array})
minute
Returns the objects current minute.
public minute({Array})
second
Returns the objects current second.
public second({Array})
add
Add a date/time interval to the current date/time.
public add({Array})
See the PHP documentation on how to use the DateInterval object.
Parameters
Parameter | Type | Description |
---|---|---|
$interval | string | Can be either a \DateInterval object or a string representing an |
interval, such as P1H to specify 1 hour |
| $returnNew
| boolean
| Doesn't update the current \Hazaar\Date object and instead returns a new object with the interval applied |
sub
Subtract a date/time interval from the current date/time.
public sub({Array})
See the PHP documentation on how to use the DateInterval object.
Parameters
Parameter | Type | Description |
---|---|---|
$interval | string | can be either a \DateInterval object or a string representing an interval, such as P1H to specify 1 hour |
$returnNew | bool | doesn't update the current \Hazaar\Date object and instead returns a new object with the interval applied |
compare
Compare the current date with the argument.
public compare({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$date | mixed | |
$includeTime | bool |
start
Returns the start time of the current date as a Date object.
public start({Array})
end
Returns the end time of the current date as a Date object.
public end({Array})
firstOfWeek
Returns the first day of the current week as a Date object.
public firstOfWeek({Array})
lastOfWeek
Returns the last day of the current week as a Date object.
public lastOfWeek({Array})
firstOfMonth
Returns the first day of the current month as a Date object.
public firstOfMonth({Array})
firstOfYear
Returns the first day of the current year as a Date object.
public firstOfYear({Array})
lastOfYear
Returns the last day of the current year as a Date object.
public lastOfYear({Array})
getFuzzyDiff
Return a fuzzy diff between the current time and the Date value.
public getFuzzyDiff({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$precise | bool | Boolean indicating if precise mode should be used. This generally adds the time |
to day-based results. |
| $dateThresholdDays
| int
| A threshold in days after which the full date will be returned. Avoids situations like "3213 days ago" which is silly. |
getLocaleData
public getLocaleData({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$locale | string | |
$ | bool |
getLocaleDateFormat
public getLocaleDateFormat({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$locale | string |
jsonSerialize
Outputs the UTC timestamp (EPOCH) When an object is included in a json_encode call.
public jsonSerialize({Array})
Generated by Hazaar API Doc Generator