<<css mode="next" class="sidebar"></css>> (((
- '''<a href="/docs/fDate">Class Documentation</a>''' - <a href="/api/fDate">API Reference</a> - <a href="https://github.com/flourishlib/flourish-classes/blob/master/fDate.php" target="_blank">Source Code</a>
<<toc></toc>>
- '''fDate''' - fTime - fTimestamp
- '''fDate''' - fMoney - fNumber - fTime - fTimestamp
)))
The fDate class is a value object representation of a date. One of the primary attributes of the object is that its value can not be changed, but instead a new object is created.
This class is built on top of the PHP date/time functions and can only handle dates ranging from 1901–2038.
The fDate constructor takes a single argument, either a string, integer or object (with a `__toString()` method) representing a date. Any string format accepted by `strtotime()` will work.
Rather than allowing an fDate object value to be modified, which can create issues since objects are passed by reference, all changes to a date create a new object.
Usually when modifying a date, only one or two components (such as month or year) of the date will change. The ::modify() method leverages the formatting codes from the `date()` function to keep parts of the existing date while replacing others.
Here are some examples of `modify()`:
Occasionally you may have the need to adjust a date. The ::adjust() method takes a single parameter which can contain any relative time measurement that `strtotime()` accepts. Since the fDate class is an immutable value object, calls to `adjust()` return a new fDate object.
To format the date, simply call the ::format() method with any valid date formatting string from `date()`. Here are some examples:
There are five different methods available to compare dates, ::eq(), ::gt(), ::gte(), ::lt() and ::lte(). Each method optionally accepts a parameter `$other_date`. If no `$other_date` is specified, the date is compared to the current date. If `$other_date` is specified, the two are compared. `$other_date` accepts any valid date descriptor that works with ::__construct().
Here are some examples:
If you are looking to get a fuzzy difference between two dates for display, you’ll want to use the ::getFuzzyDifference() method. The first parameter, `$other_date`, optionally accepts a valid date descriptor that can be passed to ::__construct(). If one is passed, the difference will be between the two dates, if nothing is passed, the difference will be between the fDate and the current date.
The value returned by `getFuzzyDifference()` will be a string representing the most broad time measurement between the two dates. In addition, if the difference is just shy of the next largest time measurement, it will be rounded up. Thus 3.5 weeks would become 1 month.
Here are some examples to clarify. The following examples are comparing two date descriptors:
These examples show output when comparing an fDate object with the current date:
An optional boolean parameter, `$simple`, can also be passed to `getFuzzyDifference()`. When `TRUE`, this parameter causes the method to return the difference in time, but not the direction.