【问题标题】:PHP calculate financial yearPHP计算财政年度
【发布时间】:2012-06-02 02:40:16
【问题描述】:

我想使用 php 从 mysql 表中获取数据来计算财政年度。

要求是计算每个财政年度(3 月 31 日至 4 月 1 日)的学生分数。是否可以制作任何每年自行计算这些日期的函数?

我的学生考试成绩表正在存储考试日期(2012 年 9 月 2 日),并且今年(2011 年 9 月 2 日)的同一学生也有旧记录。我只想输出当年的。直到现在我无法得到这个,这是我的代码:-

$result1 = mysql_query(
    "SELECT SUM(score), SUM(score_from) 
     FROM school_test_report, school_students 
     WHERE (school_test_report.student_id = school_students.student_id and
school_test_report.class=school_students.class) 
     AND school_test_report.student_id='$id'
     AND school_test_report.subject = 'maths'
    /* something here to get dates  school_test_report.test_date is between 31 march to 1 April */"
)
or die(mysql_error());  
$row = mysql_fetch_assoc($result1);
echo $row['SUM(score)'].'/'. $row['SUM(score_from)'];

它给我的不是一个财政年度的所有结果。

【问题讨论】:

  • “测试日期”;它的数据类型是什么?
  • 2-sep-2011 这种格式(varchar)(维护旧数据库)
  • 您能否包含相关表的简单架构?
  • 请停止使用古老的mysql_* 函数编写新代码。它们不再维护,社区已经开始 deprecation process 。相反,您应该了解prepared statements 并使用PDOMySQLi。如果您无法决定,this article 将帮助您选择。如果你想学习,here is a good PDO-related tutorial.
  • 您的财政年度不应该在 4 月 1 日到 3 月 31 日之间吗? 3 月 31 日至 4 月 1 日只有 1 天。

标签: php function financial


【解决方案1】:
<?php
if(date('m')=="04"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="05"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="06"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="07"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="08"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="09"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="10"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="11"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="12"){
    $fyear = date('Y').'-'.(date('y')+1);
}
 if(date('m')=="01"){
    $fyear = (date('Y')-1).'-'.(date('y'));
}
 if(date('m')=="02"){
    $fyear = (date('Y')-1).'-'.(date('y'));
}
 if(date('m')=="03"){
    $fyear = (date('Y')-1).'-'.(date('y'));
}

?>

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 link-only-answers
【解决方案2】:

很简单

$financial_year_to = (date('m') > 3) ? date('y') +1 : date('y');
$financial_year_from = $financial_year_to - 1;

$year= $financial_year_from .'-'.$financial_year_to;

【讨论】:

    【解决方案3】:

    这个帖子已经很老了,但我想在上面的 vascowhites 答案中添加我的改进。

    以下类将处理不同的会计年度,而不仅仅是北美的默认值。

    输出也更灵活一些

    class FiscalYear extends DateTime {
    
        // the start and end of the fiscal year
        private $start;
        private $end;
    
        /**
         * 
         * Create a fiscal year object
         * 
         * @param type $time date you wish to determine the fiscal year in 'yyyy-mm-dd' format
         * @param type $fiscalstart optional fiscal year start month and day in 'mm-dd' format
         */
        public function __construct($time = null, $fiscalstart = '07-01') {
            parent::__construct($time,null);
            list($m, $d) = explode('-', $fiscalstart);
            $this->start = new DateTime();
            $this->start->setTime(0, 0, 0);
            $this->end = new DateTime();
            $this->end->setTime(23, 59, 59);
            $year = $this->format('Y');
            $this->start->setDate($year, $m, $d);
            $this->end = clone $this->start;
            if ($this->start <= $this) {
                $this->end->add(new DateInterval('P1Y'));
                $this->end->sub(new DateInterval('P1D'));
            } else {
                $this->start->sub(new DateInterval('P1Y'));
                $this->end->sub(new DateInterval('P1D'));
            }
        }
    
        /**
         * return the start of the fiscal year
         * @return type DateTime
         */
        public function Start() {
            return $this->start;
        }
    
        /**
         * return the end of the fiscal year
         * @return type DateTime
         */
        public function End() {
            return $this->end;
        }
    
    }
    
    
    echo 'Date: ';
    $t1 = new FiscalYear('2015-07-02');
    echo $t1->Format('Y-m-d');
    echo '<br />Start: ';
    echo $t1->Start()->Format('Y-m-d');
    echo '<br />End: ';
    echo $t1->End()->Format('Y-m-d');
    
    echo '<br /><br />Date: ';
    $t2 = new FiscalYear('2015-06-29');
    echo $t2->Format('Y-m-d');
    echo '<br />Start: ';
    echo $t2->Start()->Format('Y-m-d');
    echo '<br />End: ';
    echo $t2->End()->Format('Y-m-d');
    
    echo '<br /><br />Date: ';
    $t3 = new FiscalYear('2015-07-02','04-01');
    echo $t1->Format('Y-m-d');
    echo '<br />Start: ';
    echo $t3->Start()->Format('Y-m-d');
    echo '<br />End: ';
    echo $t3->End()->Format('Y-m-d');
    
    echo '<br /><br />Date: ';
    $t4 = new FiscalYear('2015-06-29','04-01');
    echo $t4->Format('Y-m-d');
    echo '<br />Start: ';
    echo $t4->Start()->Format('Y-m-d');
    echo '<br />End: ';
    echo $t4->End()->Format('Y-m-d');
    

    【讨论】:

      【解决方案4】:
      /**
      * Current financial year first date where financial year starts on 1st April
      */
      $financialyeardate = 
      (date('m')<'04') ? date('Y-04-01',strtotime('-1 year')) : date('Y-04-01');
      

      【讨论】:

        【解决方案5】:

        在对日期执行计算时,最好扩展 DateTime 类。这样可以将您的所有日期计算都封装在一个地方。随着时间的推移,您将建立一个非常有用的库。

        要计算财政年度,您可以这样延长 DateTime:-

        class MyDateTime extends DateTime
        {
            /**
            * Calculates start and end date of fiscal year
            * @param DateTime $dateToCheck A date withn the year to check
            * @return array('start' => timestamp of start date ,'end' => timestamp of end date) 
            */
            public function fiscalYear()
            {
                $result = array();
                $start = new DateTime();
                $start->setTime(0, 0, 0);
                $end = new DateTime();
                $end->setTime(23, 59, 59);
                $year = $this->format('Y');
                $start->setDate($year, 4, 1);
                if($start <= $this){
                    $end->setDate($year +1, 3, 31);
                } else {
                    $start->setDate($year - 1, 4, 1);
                    $end->setDate($year, 3, 31);
                }
                $result['start'] = $start->getTimestamp();
                $result['end'] = $end->getTimestamp();
                return $result;
            }
        }
        

        这将提供一个您可以轻松包含在查询中的结果(如果可以的话,您应该真正将其更改为 mysqli 或 pdo)。

        您可以像这样使用新功能:-

        $mydate = new MyDateTime();    // will default to the current date time
        $mydate->setDate(2011, 3, 31); //if you don't do this
        $result = $mydate->fiscalYear();
        var_dump(date(DATE_RFC3339, $result['start']));
        var_dump(date(DATE_RFC3339, $result['end']));
        

        如果您希望可以修改方法以将开始和结束日期作为 DateTime 对象返回:-

        $result['start'] = $start;
        $result['end'] = $end;
        return $result;
        

        然后您可以直接格式化以包含在您的查询中:-

        $mydate = new MyDateTime();
        $mydate->setDate(2011, 3, 31);
        $result = $mydate->fiscalYear();
        $start = $result['start']->format('Y M d');
        $end = $result['end']->format('Y M d');
        

        查看date formats的手册

        【讨论】:

          【解决方案6】:

          另一个:

          SELECT UNIX_TIMESTAMP(school_test_report.test_date) AS unixdate, ...
          ...
          AND unixdate>=UNIX_TIMESTAMP('31-mar-<?php echo date("Y")-1; ?>') AND
          AND unixdate<=UNIX_TIMESTAMP('1-apr-<?php echo date("Y"); ?>')
          

          我不知道还有什么。

          【讨论】:

          • 对于第一个示例,它会计算每年 3 月 31 日至 4 月 1 日之间的日期吗?是的,第二个示例将在 2011 年 3 月 31 日至 2012 年 4 月 1 日有效,但我必须每年更改日期,有什么东西可以自行获取这些日期吗??
          • @vlzvl:正如 OP 所说 - 那里有一个 varchar 2-sep-2011,所以它不起作用
          • yaaa 我试过了,但问题是这将在 2012 年 3 月 31 日至 2012 年 4 月 1 日出现,如果我们制作 Y-1,它将在 4 月不起作用 bcos 日期将是 31- 2010 年 3 月 ...我想要 2011 年 3 月 31 日到 2012 年 4 月 1 日之间的日期(
          • yaa 不能作为同年的 Y 31-mar-2012 to 01-apr-2012 我想要 31-mar-2011 to 01-apr-2012
          • 再次查看帖子 :) 如果需要,您可以给 date("Y") 与年份混淆(date("Y")-1 将给您上一年)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多