Skip to content

Instantly share code, notes, and snippets.

@gothedistance
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gothedistance/8970727 to your computer and use it in GitHub Desktop.
Save gothedistance/8970727 to your computer and use it in GitHub Desktop.
JANコードのチェックデジットを計算するPHPコード
<?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