Some function of Date & Time

Send to friendPrinter-friendly version

Following Date & Time Function so helpfull:

1. Number of days in a month
The function 'cal_days_in_month' is used to return the number of days in a month. This function takes three arguments one for calendar name, month value and year value respectively.

Syntax:
cal_days_in_month( CAL_GREGORIAN, $intMonth, $intYear);

Example:
cal_days_in_month ( CAL_GREGORIAN, 1, 2005); Output: 31

2. Calculating Days In A Month

All developers keep a tool box of useful functions and classes that they pick up or write along the way and this PHP came to me while I was writing an event calendar page for a customer.

I should mention that PHP does offer a cal_days_in_month function for PHP builds of PHP 4.0.7 and higher, but I prefer using the below function because it is guaranteed to work on all PHP version because it is based solely on logic.

The Code:

function get_days_in_month($month, $year)  { 
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31); 
}

One line of logic provides the number of days in a month, taking into account leap years, and all on one line.

3. Checking For Leap Year Using PHP

One part of programming that seems pretty static is dealing with dates. The calendar is a set system of rules that doesn't look to change. The only part of the calendar that can be variable is a leap year, which changes every four years (obviously).

Using pure PHP ternary logic, much like the PHP Function - Calculating Days In A Month, I posted a few weeks back, you can check to see if a year is a leap year.

The Code

function is_leap_year($year = date('Y'))  {
return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %400) == 0)));
}

4. Check the date format

The function 'checkdate' is used to check whether the date passed as an argument is a valid date or not. The function returns a boolean value of '1' if the date is a valid date and 'Null' if the date is invalid.

Syntax:
checkdate ($intMonth,$date, $intYear);

Example:
i). checkdate (1, 30,2005); Output: (1 means true)
ii). checkdate (2, 30,2005); Output: (null means false)

5. Date Difference Function:

Following function differrent two date on given format:

//Time difference in day

function dateDiff($dformat, $endDate, $beginDate, $format=1) {

$date_parts1 = explode($dformat, $beginDate);

$date_parts2 = explode($dformat, $endDate);

if($format==1) {   //yyyy-mm-dd

$start_date = gregoriantojd($date_parts1[1], $date_parts1[2],

$date_parts1[0]); //mm-dd-yyyy

$end_date = gregoriantojd($date_parts2[1], $date_parts2[2],

$date_parts2[0]);

} elseif($format==2) {   //mm-dd-yyyy

$start_date = gregoriantojd($date_parts1[0], $date_parts1[1],

$date_parts1[2]); //mm-dd-yyyy

$end_date = gregoriantojd($date_parts2[0], $date_parts2[1],

$date_parts2[2]);

} elseif ($format==3) {   //dd-mm-yyyy

$start_date = gregoriantojd($date_parts1[1], $date_parts1[0],

$date_parts1[2]); //mm-dd-yyyy

$end_date = gregoriantojd($date_parts2[1], $date_parts2[0],

$date_parts2[2]);

}

return $end_date - $start_date;

}