簡體   English   中英

PHP計算最新的3月31日

[英]PHP to calculate latest 31 March

我想計算最新的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以此計算財政年度的日期... always between 31-mar-lastyear to 1-April-thisyear到今年always between 31-mar-lastyear to 1-April-thisyear ...應該自動always between 31-mar-lastyear to 1-April-thisyear這一年...

31-mar-date('y') and 31-mar-date('y')-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

這是一個使用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
?>
if (date('m')>3) {
    $year = date('Y').'-'.(date('Y')+1);
} else {
    $year = (date('Y')-1).'-'.date('Y');
}
echo $year;

這是印度的當前財政年度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM