3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /* Author: Silviu Schiau (@sschiau on Twitter) Web: www.schiau.co File: Particle.php Created: 1364036124000 (UNIX Time) Modified: 1378831167000 (UNIX Time) About: PHP implementation of Twitter Snowflake ID Generator (Extended to 42bits epoch for 96 years 1 month 21 days 16 hours 42 minutes 24 seconds in the future) License: Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt Thanks to Twitter for Snowflake. This header should NOT be removed if you want to use Particle. */ class Particle { const EPOCH=1378831167000; final public function generateParticle($machine_id) { //Time - 42 bits (millisecond precision w/ a custom epoch gives us 96 years 1 month 21 days 16 hours 42 minutes 24 seconds in the future) $time = floor(microtime(true) * 1000); //Substract custom epoch from current time $time -= self::EPOCH; //Add to base $base = pow(2,41); $base += $time; $base = decbin($base); //configured machine id - 10 bits - to 1024 machines $machineid = decbin($machine_id); //sequence number - 12 bits - up to 4096 random numbers per machine $random = mt_rand(0,pow(2,12)-1); $random = decbin($random); //Pack $base = $base.$machineid.$random; return base_convert($base,2,10); } final public function timeFromParticle($particle) { return base_convert(substr(base_convert($particle,10,2),0,42),2,10)-pow(2,41)+self::EPOCH; } } function getId($machine_id = 0, $sleep = true) { // Without sleeping 1ms two elements created at the very // same time will collide when generated on the same machine if ($sleep) { usleep(1000); } $particle = new Particle; return $particle->timeFromParticle($particle->generateParticle($machine_id)); } // We need to verify that there are no ID conflicts echo "Same machine same time\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo getId(1, false) . "\n"; echo "Different machine same time\n"; echo getId(10, false) . "\n"; echo getId(20, false) . "\n"; echo getId(30, false) . "\n"; echo getId(10, false) . "\n"; echo getId(20, false) . "\n"; echo getId(30, false) . "\n"; echo "Same machine different time\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n"; echo getId(1) . "\n";
Output for 7.1.0
Same machine same time 1448960939006 1448960939006 1448960939006 1448960939006 1448960939006 1448960939006 Different machine same time 1448960939006 1448960939006 1448960939006 1448960939006 1448960939006 1448960939006 Same machine different time 1448960939007 1448960939008 1448960939009 1448960939010 1448960939011 1448960939012
Output for 7.0.14
Same machine same time 1448960939003 1448960939003 1448960939004 1448960939004 1448960939004 1448960939004 Different machine same time 1448960939004 1448960939004 1448960939004 1448960939004 1448960939004 1448960939004 Same machine different time 1448960939005 1448960939006 1448960939007 1448960939008 1448960939009 1448960939010
Output for 5.3.19, 5.3.27, 5.4.25, 5.4.27, 5.4.37, 5.4.41, 5.4.43, 5.5.13, 5.5.24, 5.5.26, 5.6.7, 7.0.2, 7.0.8
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009
Output for 5.4.20, 5.4.34, 5.4.42, 5.5.2, 5.5.16, 5.5.19, 5.5.29, 5.6.4 - 5.6.5, 5.6.8, 5.6.12, 5.6.15, 5.6.20, 7.0.7
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 5.5.1, 5.5.7, 5.5.12, 5.5.15, 5.6.16, 7.0.6
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668010 1395792668011
Output for 7.0.5
Same machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668006 1395792668007 1395792668008 1395792668009
Output for 5.3.0, 5.3.3, 5.3.9, 5.3.16, 5.3.21 - 5.3.22, 5.4.40, 7.0.4
Same machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668008 1395792668009
Output for 5.4.21, 5.4.30, 5.4.45, 5.5.11, 5.6.17 - 5.6.18, 7.0.3
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668004 1395792668005 1395792668007 1395792668008 1395792668009 1395792668010
Output for 7.0.1
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 7.0.0
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668004 1395792668005 1395792668006 1395792668007 1395792668009 1395792668010
Output for hhvm-3.12.0
Same machine same time 1395792668152 1395792668153 1395792668153 1395792668153 1395792668153 1395792668153 Different machine same time 1395792668153 1395792668153 1395792668153 1395792668153 1395792668153 1395792668153 Same machine different time 1395792668154 1395792668155 1395792668156 1395792668157 1395792668158 1395792668159
Output for hhvm-3.10.0
Same machine same time 1395792668133 1395792668134 1395792668134 1395792668134 1395792668134 1395792668134 Different machine same time 1395792668134 1395792668134 1395792668134 1395792668134 1395792668134 1395792668134 Same machine different time 1395792668135 1395792668136 1395792668137 1395792668138 1395792668139 1395792668140
Output for 5.6.28
Same machine same time 1448960939005 1448960939005 1448960939005 1448960939005 1448960939005 1448960939005 Different machine same time 1448960939005 1448960939005 1448960939005 1448960939005 1448960939005 1448960939005 Same machine different time 1448960939006 1448960939008 1448960939009 1448960939010 1448960939011 1448960939012
Output for 5.4.29, 5.5.10, 5.6.23
Same machine same time 1395792668003 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 5.4.31, 5.4.36, 5.4.38, 5.5.35, 5.6.9, 5.6.22
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668004 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 5.5.4, 5.6.21
Same machine same time 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 Different machine same time 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 Same machine different time 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010 1395792668011
Output for 5.6.19
Same machine same time 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 Different machine same time 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 Same machine different time 1395792668008 1395792668009 1395792668010 1395792668011 1395792668012 1395792668013
Output for 5.4.15 - 5.4.16, 5.4.35, 5.5.22, 5.5.31, 5.6.14
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 5.5.14, 5.5.18, 5.5.34, 5.6.10, 5.6.13
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668008 1395792668009 1395792668010 1395792668011
Output for 5.4.23, 5.5.21, 5.6.11
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008 1395792668010
Output for 5.6.6
Same machine same time 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 Different machine same time 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 1395792668006 Same machine different time 1395792668007 1395792668008 1395792668010 1395792668011 1395792668012 1395792668013
Output for 5.5.3, 5.5.20, 5.6.3
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668007 1395792668008 1395792668009 1395792668010 1395792668011
Output for 5.4.24, 5.5.5, 5.5.9, 5.6.2
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010 1395792668011
Output for 5.6.1
Same machine same time 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 Different machine same time 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 Same machine different time 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010 1395792668012
Output for 5.4.32, 5.6.0
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 5.5.37
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668005 Same machine different time 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010 1395792668011
Output for 5.4.22, 5.4.28, 5.5.33, 5.5.36
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668011
Output for 5.5.32
Same machine same time 1395792668002 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009
Output for 5.5.30
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 5.5.28
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668005 1395792668005 Different machine same time 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 Same machine different time 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010 1395792668011
Output for 5.3.25, 5.4.18 - 5.4.19, 5.5.0, 5.5.6, 5.5.27
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Same machine different time 1395792668004 1395792668005 1395792668006 1395792668008 1395792668009 1395792668010
Output for 5.4.26, 5.5.25
Same machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 Different machine same time 1395792668003 1395792668003 1395792668003 1395792668003 1395792668003 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009 1395792668010
Output for 5.5.23
Same machine same time 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 Different machine same time 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 1395792668005 Same machine different time 1395792668006 1395792668007 1395792668008 1395792668009 1395792668011 1395792668012
Output for 5.5.8
Same machine same time 1395792668007 1395792668007 1395792668007 1395792668007 1395792668007 1395792668007 Different machine same time 1395792668007 1395792668007 1395792668007 1395792668007 1395792668007 1395792668007 Same machine different time 1395792668008 1395792668009 1395792668010 1395792668011 1395792668012 1395792668013
Output for 5.4.44
Same machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008 1395792668009
Output for 5.3.7, 5.3.12, 5.3.15, 5.3.17, 5.4.2, 5.4.13, 5.4.39
Same machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668007 1395792668008 1395792668009
Output for 5.4.17
Same machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Different machine same time 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 1395792668004 Same machine different time 1395792668005 1395792668006 1395792668007 1395792668009 1395792668010 1395792668011
Output for 5.3.2, 5.4.10, 5.4.12, 5.4.14
Same machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668009
Output for 5.2.6, 5.2.12 - 5.2.13, 5.3.4, 5.3.18, 5.3.20, 5.3.26, 5.4.0, 5.4.4, 5.4.7, 5.4.11
Same machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.1.4, 5.1.6, 5.2.1, 5.2.10, 5.2.14, 5.3.10 - 5.3.11, 5.3.23, 5.4.9
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.3.14, 5.4.8
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.2.2, 5.3.6, 5.3.24, 5.4.6
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668003 1395792668004 1395792668005 1395792668007 1395792668008
Output for 5.3.8, 5.4.5
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.4.3
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.3.1, 5.4.1
Same machine same time 1395792668001 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.3.13, 5.3.29
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.1.0, 5.2.4, 5.2.7 - 5.2.9, 5.2.11, 5.3.28
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668003 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.1.1 - 5.1.2, 5.2.15, 5.3.5
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668003 1395792668004 1395792668006 1395792668007 1395792668008
Output for 5.1.3, 5.2.5, 5.2.17
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.2.16
Same machine same time 1395792668001 1395792668001 1395792668002 1395792668002 1395792668002 1395792668002 Different machine same time 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 1395792668002 Same machine different time 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007 1395792668008
Output for 5.0.4, 5.1.5, 5.2.3
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007
Output for 5.2.0
Same machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Different machine same time 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668003 1395792668004 1395792668005 1395792668006 1395792668008
Output for 5.0.1 - 5.0.2, 5.0.5
Same machine same time 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 Different machine same time 1395792668000 1395792668001 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007
Output for 5.0.3
Same machine same time 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 Different machine same time 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 Same machine different time 1395792668002 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007
Output for 5.0.0
Same machine same time 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 1395792668000 Different machine same time 1395792668000 1395792668000 1395792668001 1395792668001 1395792668001 1395792668001 Same machine different time 1395792668002 1395792668003 1395792668004 1395792668005 1395792668006 1395792668007
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/eHKCc on line 17
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/eHKCc on line 17
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/eHKCc on line 17
Process exited with code 255.