簡體   English   中英

特定季度的php下降

[英]Php Fall on particular Quarter

抱歉,我當前在此部分遇到問題。 我的日期格式為Ymd

$date1 = '2016-03-1';
$date2 = '2016-07-1';
$date3 = '2016-10-1';
$date4 = '2016-12-1';

如果今天是2016年8月12日,則自下一個周期以來應該跌至$ date3。 我嘗試使用if條件,但它僅在第一個條件下發生。 你能啟發我嗎?

我的代碼有點像這樣

$today = date("2016-07-29");
if(strtotime($date1) < strtotime($today)){
        $next_accrual_date = $date1;
        $date_condition = 'condition 1';
    }else
    if(strtotime($date2) <   strtotime($today)){
        $next_accrual_date = $date2;
        $date_condition = 'condition 2';
    }else
    if(strtotime($date3) <  strtotime($today)){
        $next_accrual_date = $date3;
        $date_condition = 'condition 3';
    }else
    if(strtotime($date4) <  strtotime($today)){
        $next_accrual_date = $date4;
        $date_condition = 'condition 4';
    }
echo $next_accrual_date." falls to ".$date_condition;

一種方法是

<?php
$dates = array();
$dates[] = '2016-03-1';
$dates[] = '2016-07-1';
$dates[] = '2016-10-1';
$dates[] = '2016-12-1';

usort($dates, "cmp");

function cmp($a, $b){ 
    return strcmp($a, $b); 
}

foreach($dates as $date){
  if($date > "2016-08-12"){
    echo $date;
    break;
  } 
}

//print_r($dates);

?>

現場演示: https : //eval.in/621460

這將給出輸出:

 2016-10-1
    $today = strtotime(date('Y-m-d'));

    $currentYear = date('Y');
    $dates = ['q1' => strtotime($currentYear.'-03-1'), 
                'q2' => strtotime($currentYear.'-07-1'), 
                'q3' => strtotime($currentYear.'-10-1'), 
                'q4' => strtotime($currentYear.'-12-1')];

    foreach ($dates as $qName => $qDate) {
        if ($qDate > $today) {
            return "$qDate falls to $qName";
        }
    }

只需將您的比較從<更改為>:

$today = date("2016-07-29");
if(strtotime($date1) > strtotime($today)){
    $next_accrual_date = $date1;
    $date_condition = 'condition 1';
}else ...
echo $next_accrual_date." falls to ".$date_condition;

它將起作用。 當前,您查看今天是否大於第一季度的日期,然后中斷if。 但是對於這種情況,“ 2016-03-1”之后的每個日期都是正確的。 這就是為什么您總是得到條件1的原因。

暫無
暫無

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

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