获取最近3个月从当前月使用php


get last 3 months from current month using php

我想从当前月份获得最近3个月的名称。例如,当前月份是8月。我想要这些6月7月8月的数据。我试过这个代码echo date("F", strtotime("-3 months"));。它只返回6月份。我如何得到最近3个月从当前月使用php?

使用date和strtotime函数实际上是在正确的轨道上。将其扩展到您的要求:

function getMonthStr($offset)
{
    return date("F", strtotime("$offset months"));
}
$months = array_map('getMonthStr', range(-3,-1));

简单代码:

<?php
for($x=2; $x>=0;$x--){
echo date('F', strtotime(date('Y-m')." -" . $x . " month"));
echo "<br>";
}
?>

从LTroubs那里得到的灵感

应该可以了:

function lastThreeMonths() {
    return array(
        date('F', time()),
        date('F', strtotime('-1 month')),
        date('F', strtotime('-2 month'))
    );
}
var_dump(lastThreeMonths());

试试这个:-

$current_date = date('F');
for($i=1;$i<3;$i++)
{
    ${"prev_mont" . $i} = date('F',strtotime("-$i Months"));
    echo ${"prev_mont" . $i}."</br>";
}
echo $current_date."</br>";