Since at the start of 2007 all books will no longer be using the ISBN10, we needed a way to convert all those ISBN10 numbers to the new ISBN13 (or EAN).
function genchksum13($isbn) {
$isbn = trim($isbn);
for ($i = 0; $i <= 12; $i++) {
$tc = substr($isbn, -1, 1);
$isbn = substr($isbn, 0, -1);
$ta = ($tc*3);
$tci = substr($isbn, -1, 1);
$isbn = substr($isbn, 0, -1);
$tb = $tb + $ta + $tci;
}
$tg = ($tb / 10);
$tint = intval($tg);
if ($tint == $tg) { return 0; }
$ts = substr($tg, -1, 1);
$tsum = (10 - $ts);
return $tsum;
}
function isbn10_to_13($isbn) {
$isbn = trim($isbn);
if(strlen($isbn) == 12){ // if number is UPC just add zero
$isbn13 = '0'.$isbn;}
else
{
$isbn2 = substr("978" . trim($isbn), 0, -1);
$sum13 = genchksum13($isbn2);
$isbn13 = "$isbn2$sum13";
}
return ($isbn13);
}
// usage //
echo isbn10_to_13('1234567890'); // returns ISBN13
There are no checks in the code to see if the number is numeric for example, but this should give you a good place to start.
Similar Posts:
- Find Time Between Two Dates in PHP
- Find Years and Months Between Two Dates in PHP
- UPDATED: Strip Off Characters from String Using PHP (substr)
- Turn 2D Array into a HTML Dropdown with PHP
- Text to Image Using PHP and img4me


October 8th, 2008 @ 5:45 pm
Thank you!!
February 12th, 2009 @ 3:34 am
thank you very much ..
April 26th, 2010 @ 1:54 pm
Excellent – thank you!
November 8th, 2010 @ 6:31 pm
$tb is undefined at first. Should it be 0? or some other number
June 22nd, 2011 @ 5:11 pm
<?php
$isbn = '978' . substr($isbn10, 0, -1);
for ($i = 0; $i 0)
$checkDigit = 10 - $checkDigit;
$isbn .= $checkDigit;
echo('ISBN-13: ' . $isbn);
?>
July 5th, 2011 @ 8:40 am
Thank you very much. Works perfectly. You saved my time as i was going to write it myself.
August 17th, 2011 @ 2:11 pm
This works great – Thank you!