function date_calc_begin_of_month_by_span in Date 5.2
Same name and namespace in other branches
- 6.2 date_php4/date_php4_calc.inc \date_calc_begin_of_month_by_span()
- 6 date_php4/date_php4_calc.inc \date_calc_begin_of_month_by_span()
Returns date of the first day of the month in the number of months from the given date
Parameters
int $months: The number of months from the date provided. Positive numbers go into the future. Negative numbers go into the past. 0 is the month presented in $month.
int $month: The month.
int $year: The 4 digit year. Do not add leading 0's for years prior to 1000.
string $format: The string indicating how to format the output.
Return value
string The date in the desired format.
1 string reference to 'date_calc_begin_of_month_by_span'
- date_php4.inc in date_php4/
date_php4.inc
File
- date_php4/
date_php4_calc.inc, line 1210
Code
function date_calc_begin_of_month_by_span($months = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
if (empty($year)) {
$year = date_calc_get_year();
}
if (empty($month)) {
$month = date_calc_get_month();
}
if ($months > 0) {
// future month
$tmp_mo = $month + $months;
$month = $tmp_mo % 12;
if ($month == 0) {
$month = 12;
$year = $year + floor(($tmp_mo - 1) / 12);
}
else {
$year = $year + floor($tmp_mo / 12);
}
}
else {
// past or present month
$tmp_mo = $month + $months;
if ($tmp_mo > 0) {
// same year
$month = $tmp_mo;
}
elseif ($tmp_mo == 0) {
// prior dec
$month = 12;
$year--;
}
else {
// some time in a prior year
$month = 12 + $tmp_mo % 12;
$year = $year + floor($tmp_mo / 12);
}
}
return date_calc_format(1, $month, $year, $format);
}