0

最新の3月31日を計算したい....日付が2012年1月1日であると仮定します。2011年3月31日の結果が必要です。2011年4月1日の場合は、2011年3月31日の結果も必要です。 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

しかし、毎回今年度のように機能しているわけではありません。

4

3 に答える 3

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ます。

于 2012-05-27T09:34:17.553 に答える
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
?>
于 2012-05-27T09:49:29.080 に答える
1
if (date('m')>3) {
    $year = date('Y').'-'.(date('Y')+1);
} else {
    $year = (date('Y')-1).'-'.date('Y');
}
echo $year;

これは、インドの現在の会計年度を取得するためのものです

于 2016-12-07T07:10:04.027 に答える