Advertisement
eyuprog

Automatic Code

Mar 28th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <?php
  2. class Autocode
  3. {
  4.    
  5.     public function code_increment($prefix='',$count=4,$last_kode='')
  6.     {
  7.         $output='';
  8.         $next='';
  9.         if(empty($last_kode))
  10.         {
  11.             $order=1;
  12.             $next=sprintf("%0".$count."s",$order);
  13.             $output=$prefix.$next;
  14.         }else{
  15.             $lenght_prefix=strlen($prefix);
  16.             $next=(int) substr($last_kode,$lenght_prefix,$count);
  17.             $next++;
  18.             $next=sprintf("%0".$count."s",$next);
  19.             $output=$prefix.$next;
  20.         }
  21.         return $output;
  22.     }
  23.    
  24.     public function code_date($prefix='',$format='Ymd',$timezone='Asia/Jakarta')
  25.     {
  26.         date_default_timezone_set($timezone);
  27.         $date=date($format);
  28.         $generate=$prefix.$date;
  29.         return $generate;
  30.     }
  31.    
  32.     public function code_random($prefix='',$length=20,$alphabet=FALSE)
  33.     {
  34.         $output='';
  35.         if(empty($alphabet) || $alphabet==FALSE)
  36.         {
  37.             $output= substr(str_shuffle("0123456789"), 0, $length);
  38.         }else{
  39.             $output= substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
  40.         }
  41.  
  42.         $generate=$prefix.$output;
  43.         return $generate;
  44.     }
  45. }
  46.  
  47. //TEST
  48. <?php
  49. require_once('Autocode.php');
  50. //RANDOM
  51. echo "----RANDOM----".'<br/>';
  52. echo "----WITHOUT PREFIX".'<br/>';
  53. echo $this->autocode->code_random('',10,TRUE).'<br/>';
  54. echo "----PREFIX".'<br/>';
  55. echo $this->autocode->code_random("CR-",40,TRUE).'<br/>';
  56. $random=$this->autocode->code_random("CR-",40,TRUE);
  57. echo strtoupper($random).'<br/>';
  58. echo '<p>&nbsp;</p>';
  59.  
  60. //DATE
  61. echo "----DATE----".'<br/>';
  62. echo "----WITHOUT PREFIX".'<br/>';
  63. echo $this->autocode->code_date('','Ymd').'<br/>';
  64. echo "----PREFIX".'<br/>';
  65. echo $this->autocode->code_date("CD/",'YmdH').'<br/>';
  66. echo $this->autocode->code_date("CD/",'dmYHis').'<br/>';
  67. echo '<p>&nbsp;</p>';
  68.  
  69. //INCREMENT
  70. echo "----INCREMENT----".'<br/>';
  71. $last_code="CI-2345677";
  72. $count=7; //523049 6 character
  73. echo "----WITHOUT PREFIX".'<br/>';
  74. echo $this->autocode->code_increment('',6).'<br/>';
  75. echo "----PREFIX Last Code ".$last_code.'<br/>';
  76. echo $this->autocode->code_increment('CI-',$count,$last_code);
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement