Skip to content

Instantly share code, notes, and snippets.

@yousan
Last active October 22, 2015 06:53
Show Gist options
  • Save yousan/c14c9ec44c7ff63b6e42 to your computer and use it in GitHub Desktop.
Save yousan/c14c9ec44c7ff63b6e42 to your computer and use it in GitHub Desktop.
<?php
// 年度が始まる日
define('ANNUAL_START_DAY', '04-02');
/**
* 「年度」を取得する
*
*/
function get_annual_year(DateTime $date) {
//$annual = DateInterval::createFromDateString(ANNUAL_START_DAY);
$year = $date->format('Y');
$annual = new DateTime($year . '-'.ANNUAL_START_DAY);
$annual->sub(DateInterval::createFromDateString('1 second'));
$hoge = $date->diff($annual);
//var_dump($hoge);
if ($hoge->invert) {
//var_dump($year);
return $year;
} else {
//var_dump($year-1);
return $year-1;
}
}
test();
function main() {
}
function test() {
$theDay = new DateTime('2015-04-01');
if (get_annual_year($theDay) == '2014') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
$theDay = new DateTime('2015-04-02');
if (get_annual_year($theDay) == '2015') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
$theDay = new DateTime('2015-03-31');
if (get_annual_year($theDay) == '2014') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
$theDay = new DateTime('1970-03-31');
if (get_annual_year($theDay) == '1969') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
$theDay = new DateTime('1970-04-01');
if (get_annual_year($theDay) == '1969') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
$theDay = new DateTime('1970-04-02');
if (get_annual_year($theDay) == '1970') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
$theDay = new DateTime('1970-12-31');
if (get_annual_year($theDay) == '1970') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
$theDay = new DateTime('1970-1-1');
if (get_annual_year($theDay) == '1969') { echo 'OK!'.PHP_EOL; }
else { echo 'NG!'.PHP_EOL; }
//$theDay = new DateTime('1970-03-33');
//if (get_annual_year($theDay) == '1970') { echo 'OK!'.PHP_EOL; }
//else { echo 'NG!'.PHP_EOL; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment