Table of Contents
Date manipulation can be challenge due to the fact that there are many common date entry formats and database date formats. Marmot's MU_Date class is designed to help with date manipulation.
The MU_Date class is simple to use. At a minimum, it takes a date string and a format to use for parsing the date. For example:
$date = new MU_Date('4/15/2004', '%m/%d/%Y');
An MU_Date object has the following attributes:
Table 7.1. MU_Date Attributes
| Attribute | Description |
|---|---|
| $date->timestamp | Contains the Unix timestamp of the current date value (seconds since the epoch: Jan 1, 1970 at 00:00:00 GMT) |
| $date->valid | True if the current date is valid, false otherwise |
| $date->string | Contains the original string that was used to create the date |
| $date->year | Contains the year value of the current date |
| $date->month | Contains the month value of the current date |
| $date->day | Contains the day value of the current date |
| $date->hour | Contains the hour value of the current date |
| $date->minute | Contains the minute value of the current date |
| $date->second | Contains the second value of the current date |
It has the following methods:
Table 7.2. MU_Date Methods
| Method | Description | Parameters | Returns |
|---|---|---|---|
| $date->format($format) | Sets the format of the object | format - a valid date format (described below) | the current format |
| $date->year($year) | Sets the year of the object | year - a valid year value | the current year |
| $date->month($month) | Sets the month of the object | month - a valid month value (1-12) | the current month |
| $date->day($day) | Sets the day of the object | day - a valid day value (1-31) | the current day |
| $date->hour($hour) | Sets the hour of the object | hour - a valid hour value | the current hour |
| $date->minute($minute) | Sets the minute of the object | minute - a valid minute value | the current minute |
| $date->second($second) | Sets the second of the object | second - a valid second value | the current second |
| $date->parse_date($date, $format) | Parses the date uses the format specified. It sets all object attributes to the newly parsed date | date - a string representing a date format - optional, a valid format string, defaults to the value of the object format | true if the value successfully parsed as a valid date, false otherwise |
| $date->compare($compare_date) | Compares the current date object value to the value of the compare_date object | compare_date - another date object | 0 if the dates are equal, 1 if this date is greater than the compare date (ie, this is after that), -1 if this date is less than the compare date (ie, this is before that) |
| $date->calculate($interval) | Calculates the difference using the current date and interval | interval - the interval string, any string acceptable to the PHP strtotime function | the new date formatted using the the format of the current object |
| $date->format_date($format) | Formats the current date using the specified format | format - optional, a valid format, defaults to the current object format | the formatted date |
The following format specifiers are supported: