PHP 计算最新 3 月 31 日


PHP to calculate latest 31 March

我想计算最新的 31-Mar ....假设日期是 1-Jan-2012 我希望结果为 31-Mar-2011,如果是 1-4-2011,那么我也想要结果 31-Mar-2011,如果它的 1-Mar-2011 它应该是 31-Mar-2010.....希望我把自己说清楚了...(使用PHP)...我计算财政年度的日期... always between 31-mar-lastyear to 1-April-thisyear ...年份应该自动采取...我像这样尝试

31-mar-date('y') and 31-mar-date('y')-1

但它不是每次都像今年那样工作。

下面是一个使用 php 精彩的 strtotime 函数的示例。

<?php
$day = 1;
$month = 1;
$year = 2011;
$date = mktime(12, 0, 0, $month, $day, $year);
$targetMonth = 3;
$difference = $month - $targetMonth;
if($difference < 0) {
    $difference += 12;
}
$sameDateInMarch = strtotime("- " . $difference . " months", $date);
echo "Entered date: " . date("d M Y", $date) . "<br />";
echo "Last 31 march: " .  date("t M Y", $sameDateInMarch);
// the parameter 't' displays the last day of the month
?>

像这样:

function getLast() {
    $currentYear = date('Y');
    // Check if it happened this year, AND it's not in the future.
    $today = new DateTime();
    if (checkdate(3, 31, $currentYear) && $today->getTimestamp() > mktime(0, 0, 0, 3, 31, $currentYear)) {
        return $currentYear;
    }
    while (--$currentYear) {
        if (checkdate(3, 31, $currentYear)) {
            return $currentYear;
        }
    }
    return false;
}
var_dump(getLast());

它应该返回年份或false

if (date('m')>3) {
    $year = date('Y').'-'.(date('Y')+1);
} else {
    $year = (date('Y')-1).'-'.date('Y');
}
echo $year;

这是为了获得印度当前的财政年度