Skip to content

Instantly share code, notes, and snippets.

@mamor
Created February 7, 2013 02:52
Show Gist options
  • Save mamor/4728011 to your computer and use it in GitHub Desktop.
Save mamor/4728011 to your computer and use it in GitHub Desktop.
指定月にN月加算/減算して、その初日/末日を取得するメソッド
/**
* Get first day on target month
*
* @param string $yyyymm
* @param int $add_month
* @param string $fmt
* @return string
*/
public static function get_first_day($yyyymm = null, $add_month = 0, $fmt = 'Y-m-d')
{
! $yyyymm and $yyyymm = date('Ym');
$date = date_create($yyyymm.'01');
date_add($date, date_interval_create_from_date_string($add_month.' month'));
return date_format($date, $fmt);
}
/**
* Get last day on target month
*
* @param string $yyyymm
* @param int $add_month
* @param string $fmt
* @return string
*/
public static function get_last_day($yyyymm = null, $add_month = 0, $fmt = 'Y-m-d')
{
! $yyyymm and $yyyymm = date('Ym');
$date = date_create($yyyymm.'01');
date_add($date, date_interval_create_from_date_string(($add_month + 1).' month'));
date_sub($date, date_interval_create_from_date_string('1 day'));
return date_format($date, $fmt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment