32位和64位PHP和MySQL裡的整型範圍

來源:互聯網
上載者:User

32位和64位PHP和MySQL裡的整型範圍

一個位元組有8位,所以32位int型佔用32位/8位=4個位元組,64位int型佔用64位/8位=8個位元組.

32位,64位無符號整型最大值:
2^64-1 = 18446744073709551615
 2^32-1 = 4294967295

 32位,64位有符號整型最大值:
 (2^32)/2-1 = 2147483647
 (2^64)/2-1 = 9223372036854775807

減1是因為整型包括0.

 64位Ubuntu 14.04,PHP_INT_MAX的值為9223372036854775807,跟MySQL中有符號的bigint型的最大值一樣.
32位Ubuntu 14.04,PHP_INT_MAX的值為2147483647,跟MySQL中有符號的int型的最大值一樣.
echo date('Y-m-d H:i:s', PHP_INT_MAX); 返回 2038-01-19 11:14:07
 echo strtotime('2038-01-19 11:14:07'); 返回 2147483647
 echo strtotime('2038-01-19 11:14:08'); 32位下返回空
 也就是說,32位系統上PHP的time()最大隻能返回2038-01-19 11:14:07的時間戳記.

欄位類型: `posted` int(10) unsigned NOT NULL DEFAULT '0'
 32位MySQL上(64位MySQL也是如此),插入一個比32位無符號int型最大值 2^32-1 = 4294967295 更大的數會發生錯誤:
UPDATE `punbb`.`pb_topics` SET `posted` = '4294967296' WHERE `pb_topics`.`id` = 1;
 Warning: #1264 Out of range value for column 'posted' at row 1
不過,MySQL可以用8個位元組的bigint類型來儲存64位整數.

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.