Documentation: DateHelper: A custom Helper
DateHelper is a custom Helper that simplifies translations on dates and times in string format without the need to convert to Timestamps. The helper has been added as a package to the hdCalendar project.
All times should be provided in Y-m-d H:i:s format, or an equivalent thereof. Some actions take yearweek strings that should be formatted like “2006w29” ( as generated by date(”Y\wW”) )
DateHelper::date($date = NULL, $format = 'Y-m-d')
Verifies that a date is legitimately formatted, filling in the blanks where necessary. Does rudimentary tests.
- If year is less than 4 digits, assume it’s corrupt and use this year
- If month is missing, use this month
- If day is missing, use 01
Do a full date() calculation on the passed info and return in the format specified. It always passes 00:00:00 for time. For more accurate time, use DateHelper::datetime()
Example:
echo $date->date('2006-07');
Results:
2006-07-01
DateHelper::datetime($datetime = NULL, $format = 'Y-m-d H:i')
Relies on DateHelper::date() to handle the date portion. Does rudimentary tests.
- If no time exists in the string, use 00:00:00 for the time
- If no minute is in the string, use 00 for the minute
- If no seconds, use 00
Drop the seconds altogether. This can be changed if it’s requested, but
the initial reasoning behind the helper was to assist in scheduling where
this isn’t necessary. I’m open for suggestions. Limitation removed in v.0.1.0.3
Example:
echo $date->datetime('2006-07-18');
Results:
2006-07-18 00:00
DateHelper::adjustDate($datetime, $string, $format = 'Y-m-d')
Pass the date and modifying string to timetostr() and return in the format requested.
Example:
echo $date->adjustDate('2006-07-18 22:05', '+23 hours', 'Y-m-d H');
Results:
2006-07-19 21
DateHelper::dateDiff($first, $second)
Get the number of days between two passed days, rounded to the nearest day. This is more for convenience. A prettier result can be found in the TimeHelper that is standard in CakePHP.
Example:
echo $date->dateDiff('2006-07-18 22:05', '2006-07-19 21:05');
Results:
1
DateHelper::getDatetime($datetime)
Passes back an array from getdate(), without the ‘0’ and ‘seconds’
fields. Again, this can be modified if anyone sees fit.
DateHelper::weekday($weekday, $date, $format = 'Y-m-d')
Returns the requested weekday that is in the same week as the date. $weekday is a value from 1 - 6, where 0 = Sunday. A useful tool when building calendars and schedules.
Example:
echo $date->weekday(1, '2006-07-18'); // '2006-07-18' is a Tuesday
Results:
2006-07-17 - the Monday of the same calendar week
DateHelper::week($date, $format = '%4uw%02u')
Returns the year and week of a specific date. It also starts the week with Sunday rather than Monday, making it easier to do U.S. style calendars. This is equivalent to passing the ‘2’ flag to MySql’s YEARMONTH. Future versions may have another parameter that turns this feature on and off if enough people request it.
Example:
echo $date->week('2006-07-18');
Results:
2006w29
DateHelper::weekdayOfWeek($weekday, $yearweek, $format = 'Y-m-d')
Similiar to DateHelper::weekday(), but takes a yearweek instead of a day as the source.
Example:
echo $date->weekday(2, '2006w29');
Results:
2006-07-18 - the Tuesday of week 29 of 2006
DateHelper::adjustWeek($yearweek, $count, $format = '%4uw%02u')
Similiar to DateHelper::adjustDate() but takes a yearweek as an entry and only works in week offsets, returning a new week
Example:
echo $date->adjustWeek('2006w29', -1);
Results:
2006w28
DateHelper::daysOfWeek($yearweek, $format = 'Y-m-d')
Get an array of all of the days of the week. Again, the week starts with Sunday.
DateHelper::monthOfWeek($yearweek, $format = 'Y-m')
Choose the month that this week has the most days in. Basically, what month is the Wednesday of this week in?
Example:
echo $date->monthOfWeek('2006w26');
Results:
2006-06 - more days of this week fall in June than in July
DateHelper::lastDayOfMonth($yearmonth, $format = 'Y-m-d')
Return the last day of the month.
Example:
echo $date->lastDayOfMonth('2006-02');
Results:
2006-02-28
DateHelper::weeksOfMonth($yearmonth, $format = '%4uw%02u')
Get an array of all of the weeks of the month.
DateHelper::isWeek($string)
Perform a boolean test on a string to see if it looks like a yearweek. Very rudimentary test to see if the 5th character is a ‘w’