如何确定过去N月内各自然月的起始和结束时间戳?
- 内容介绍
- 文章标签
- 相关推荐
本文共计365个文字,预计阅读时间需要2分钟。
python计算过去N个月每个自然月的起始和结束时间,包括当前月份。@param integer $max 月份数,默认为6@param string $monthstr 返回值数组的第三个下标,表示整体月份的表达形式(如:2023-01)@return array 每个子数组包含两个元素,分别为月份的起始和结束日期(如:['2023-01-01', '2023-01-31'])def calculate_month_dates(max_months=6, monthstr=''): from datetime import datetime, timedelta import calendar
current_date=datetime.now() end_date=current_date.replace(day=calendar.monthrange(current_date.year, current_date.month)[1]) start_date=current_date.replace(day=1)
dates=[] for _ in range(max_months): start_month=start_date.strftime('%Y-%m-01') end_month=(end_date - timedelta(days=1)).strftime('%Y-%m-%d') dates.append([start_month, end_month]) start_date=start_date.replace(month=start_date.month - 1) end_date=end_date.replace(month=end_date.month - 1)
return dates
/** * 计算过去N月每个自然月的起止时间戳,包括当前月份 * @param integer $max 月份数,默认为6 * @param string $monthstr 返回值数组的第三个下标(具体月份的表示形式) * @return array 每个子数组形式array(月起始时间戳,月结束时间戳,月份名称) */ function month_offset($max= 6,$monthstr = 'ym'){ if ($max<=0) { return false; } $base = date('Y-m-01'); $y = 1; $mo = array(); $mo[0][0] = strtotime($base); $mo[0][1] = time(); $mo[0][2] = date('ym'); while ($y < $max) { $mo[$y][0] = strtotime(date('Y-m-01',strtotime($base.' -'.$y.' month'))); $mo[$y][1] = $y == 1?$mo[0][0]:$mo[$y-1][0]; $mo[$y][2] = date($monthstr,$mo[$y][0]); $y++; } return array_reverse($mo); } var_export(month_offset(3)); exit; /*array ( 0 => array ( 0 => 1425139200, 1 => 1427817600, 2 => '1503', ), 1 => array ( 0 => 1427817600, 1 => 1430409600, 2 => '1504', ), 2 => array ( 0 => 1430409600, 1 => 1430702254, 2 => '1505', ), )[Finished in 0.1s]*/
本文共计365个文字,预计阅读时间需要2分钟。
python计算过去N个月每个自然月的起始和结束时间,包括当前月份。@param integer $max 月份数,默认为6@param string $monthstr 返回值数组的第三个下标,表示整体月份的表达形式(如:2023-01)@return array 每个子数组包含两个元素,分别为月份的起始和结束日期(如:['2023-01-01', '2023-01-31'])def calculate_month_dates(max_months=6, monthstr=''): from datetime import datetime, timedelta import calendar
current_date=datetime.now() end_date=current_date.replace(day=calendar.monthrange(current_date.year, current_date.month)[1]) start_date=current_date.replace(day=1)
dates=[] for _ in range(max_months): start_month=start_date.strftime('%Y-%m-01') end_month=(end_date - timedelta(days=1)).strftime('%Y-%m-%d') dates.append([start_month, end_month]) start_date=start_date.replace(month=start_date.month - 1) end_date=end_date.replace(month=end_date.month - 1)
return dates
/** * 计算过去N月每个自然月的起止时间戳,包括当前月份 * @param integer $max 月份数,默认为6 * @param string $monthstr 返回值数组的第三个下标(具体月份的表示形式) * @return array 每个子数组形式array(月起始时间戳,月结束时间戳,月份名称) */ function month_offset($max= 6,$monthstr = 'ym'){ if ($max<=0) { return false; } $base = date('Y-m-01'); $y = 1; $mo = array(); $mo[0][0] = strtotime($base); $mo[0][1] = time(); $mo[0][2] = date('ym'); while ($y < $max) { $mo[$y][0] = strtotime(date('Y-m-01',strtotime($base.' -'.$y.' month'))); $mo[$y][1] = $y == 1?$mo[0][0]:$mo[$y-1][0]; $mo[$y][2] = date($monthstr,$mo[$y][0]); $y++; } return array_reverse($mo); } var_export(month_offset(3)); exit; /*array ( 0 => array ( 0 => 1425139200, 1 => 1427817600, 2 => '1503', ), 1 => array ( 0 => 1427817600, 1 => 1430409600, 2 => '1504', ), 2 => array ( 0 => 1430409600, 1 => 1430702254, 2 => '1505', ), )[Finished in 0.1s]*/

