0

i want to calculate latest 31-Mar .... suppose date is 1-Jan-2012 i want result as 31-mar-2011 and if is 1-April-2011 then also i want result 31-mar-2011 and if its 1-mar-2011 it should come as 31-mar-2010.....hope i made my self clear ...(with php) ... i al calculating date with this for financial year ... always between 31-mar-lastyear to 1-April-thisyear ... year should be taken automatically ... i was trying like this

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

but its not working as its taking current year every time.

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 回答