Php 如何获取上个月和下个月的数据?
如何获取前一个月Php 如何获取上个月和下个月的数据?,php,date,Php,Date,如何获取前一个月2010-09和下一个月2010-11?您只需将1添加到当前月份,然后查看您是否跨过了这一年: $year = 2010; $month = 10; // PHP > 5.2.0 $date = new DateTime(); $date->setDate($year, $month, 1); $prevDate = $date->modify('-1 month'); $nextDate = $date->modify('+1 month'); //
2010-09
和下一个月2010-11
?您只需将1
添加到当前月份,然后查看您是否跨过了这一年:
$year = 2010;
$month = 10;
// PHP > 5.2.0
$date = new DateTime();
$date->setDate($year, $month, 1);
$prevDate = $date->modify('-1 month');
$nextDate = $date->modify('+1 month');
// some $prevDate->format() and $nextDate->format()
同样,对于上个月,您可以执行以下操作:
$next_year = $year;
$next_month = ++$month;
if($next_month == 13) {
$next_month = 1;
$next_year++;
}
PHP在这方面非常棒,它将通过为您更正日期来处理日期溢出
$prev_year = $year;
$prev_month = --$month;
if($prev_month == 0) {
$prev_month = 12;
$prev_year--;
}
$PreviousMonth=mktime(0,0,0,$month-1,1,$year);
$CurrentMonth=mktime(0,0,0,$month,1,$year);
$NextMonth=mktime(0,0,0,$month+1,1,$year);
echo“下个月是”。日期('Ym',$NextMonth)。
“上个月是”。日期('Ym',$PreviousMonth.'';
strftime*:-*根据区域设置设置时间和/或日期的格式。月份和工作日名称以及其他依赖于语言的字符串与使用setlocale()设置的当前区域设置有关
试着这样做:
strftime( '%B %Y', strtotime( '+1 month', $date ) );
strftime( '%B %Y', strtotime( '-1 month', $date ) );
$date = mktime(0, 0, 0, $month, 1, $year);
echo date("Y-m", strtotime('-1 month', $date));
echo date("Y-m", strtotime('+1 month', $date));
或者,更短,像这样:
strftime( '%B %Y', strtotime( '+1 month', $date ) );
strftime( '%B %Y', strtotime( '-1 month', $date ) );
$date = mktime(0, 0, 0, $month, 1, $year);
echo date("Y-m", strtotime('-1 month', $date));
echo date("Y-m", strtotime('+1 month', $date));
你能进一步解释一下吗!天哪!你知道PHP可以帮你做这件事吗?@RS-我对你的5.2示例没有任何问题……这是上一个。
$date = mktime(0, 0, 0, $month, 1, $year);
echo date("Y-m", strtotime('-1 month', $date));
echo date("Y-m", strtotime('+1 month', $date));
echo date("Y-m", mktime(0, 0, 0, $month-1, 1, $year));
echo date("Y-m", mktime(0, 0, 0, $month+1, 1, $year));
echo date('Y-m-d', strtotime('next month'));
setlocale(LC_TIME,"turkish");
$Currentmonth=iconv("ISO-8859-9","UTF-8",strftime('%B'));
$Previousmonth=iconv("ISO-8859-9","UTF-8",strftime('%B',strtotime('-1 MONTH')));
$Nextmonth=iconv("ISO-8859-9","UTF-8",strftime('%B',strtotime('+1 MONTH')));
echo $Previousmonth; // Şubat /* 2017-02 */
echo $Currentmonth; // Mart /* 2017-03 */
echo $Nextmonth; // Nisan /* 2017-04 */