I have done this before using a paid database subscription which is updated monthly.
The results are only as accurate as the database of IP addresses, which are leased to carriers in blocks, and are traded when carriers merge, go out of business, down size, etc...
Depending on the accuracy, and other information you expect to have returned by the code will determine the cost of the database subscription, which you will need to purchase.
The code to return the approximate location from the database will take me about a day to put together, Java, and PHP versions will be delivered.
Procuring the database, and gathering any other information e.g. server access & Installing it on your machine will take about 2 days.
Regards,
John H. Ward, Jr.