Last active
August 29, 2015 13:56
-
-
Save gothedistance/8970727 to your computer and use it in GitHub Desktop.
JANコードのチェックデジットを計算するPHPコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function calcJanCodeDigit($num) { | |
$arr = str_split($num); | |
$odd = 0; | |
$mod = 0; | |
for($i=0;$i<count($arr);$i++){ | |
if(($i+1) % 2 == 0) { | |
//偶数の総和 | |
$mod += intval($arr[$i]); | |
} else { | |
//奇数の総和 | |
$odd += intval($arr[$i]); | |
} | |
} | |
//偶数の和を3倍+奇数の総和を加算して、下1桁の数字を10から引く | |
$cd = 10 - intval(substr((string)($mod * 3) + $odd,-1)); | |
//10なら1の位は0なので、0を返す。 | |
return $cd === 10 ? 0 : $cd; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment