Cara menggunakan php ean-13 check digit

Feb 15, 2008 · 2 minute read
Category: phpbarcode

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

EAN13 is a barcode format. It consists of 12 numbers which you will generally have a range assigned to you. The 13th digit is called the check digit and is formulated by loooking at the other 12 digits. The purpose of the check digit is to ensure that the number is being read correctly as if any of the numbers do not match up, the check digit will not validate.

If you need to create check digits in PHP, here is my handy function:

function ean13_check_digit($digits){
//first change digits to a string so that we can access individual numbers
$digits =(string)$digits;
// 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
$even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11};
// 2. Multiply this result by 3.
$even_sum_three = $even_sum * 3;
// 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
$odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10};
// 4. Sum the results of steps 2 and 3.
$total_sum = $even_sum_three + $odd_sum;
// 5. The check character is the smallest number which, when added to the result in step 4,  produces a multiple of 10.
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;
return $digits . $check_digit;
}

Other Barcode Related Resources:

http://phpclasses.fonant.com/browse/package/3643.html

http://thinkabdul.com/2007/01/04/barcoder-freeware-ean-13-barcode-reader-for-java-j2me-mobiles/

Calculate check digits for EAN13,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function ean13_check_digit($digits) {

$digits =(string)$digits;

$even_sum = $digits{1} + $digits{3} + $digits{5}
+ $digits{7} + $digits{9} + $digits{11};

$even_sum_three = $even_sum * 3;

$odd_sum = $digits{0} + $digits{2} + $digits{4}
+ $digits{6} + $digits{8} + $digits{10};

$total_sum = $even_sum_three + $odd_sum;


$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;
return $digits . $check_digit;
}

Reference

http://www.edmondscommerce.co.uk/blog/php/ean13-barcode-check-digit-with-php/

ean13

This package provides few functions that calculate EAN13 digits and checksum digit. It does not generate barcode images (and it will stay that way). You can use this package to generate barcode image using calculated value.

It uses https://github.com/ronanguilloux/IsoCodes to validate generated codes. checkdigit function was taken from here: https://edmondscommerce.github.io/php/barcode/ean13-barcode-check-digit-with-php.html

I hope this package helps you.

generate code

use function Newride\EAN13\create;

create('123'); // 0000000001236

generate check digit only

use function Newride\EAN13\checkdigit;

checkdigit('123'); // 6