我如何找出去年四月的一年,特别是去年四月的 1 日


How do I find out the year last April, specifically the 1st of the last April in php

在php中,我需要找到去年四月的年份。这样我就可以从该日期开始执行 mysqli 查询。我想要的日期是去年4月1日。问题是你可能在下个月,或者明年。例如,去年 4 月可能是 2015 年,而您在 2016 年 1 月。好的,这是我尝试过的代码,但显然它不起作用。

    $datestring='first day of last april';
$date=date_create($datestring);
echo nulldate_full($date->format('Y-m'));

我已经尝试了很多变体,但似乎没有任何效果。我也在寻找堆栈溢出的答案,但没有取得多大成功。

这将起作用,尽管它需要一些代码行:

// first of April this year
$april_this_year = date('Y-04-01');
// current date
$today = date('Y-m-d');
// current year
$year = date('Y');
// if April in this year is yet to come   
if ($april_this_year >= $today) {
    // then it was previous year
    --$year;
}
// show it
echo $year;

请注意,如果我们目前在四月,上述代码会将"去年四月"视为今年。