PHP最新计算3月31日

时间:2012-05-27 09:20:54

标签: php date financial

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

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

但是每次都没有按照当前的年份工作。

3 个答案:

答案 0 :(得分:1)

这样的事情:

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

答案 1 :(得分: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
?>

答案 2 :(得分:1)

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

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