Generate Random 11 Digit Number

need help…

i have this function




protected function beforeValidate() {

        parent::beforeValidate();

        if ($this->isNewRecord) {

            $criteria = new CDbCriteria; 

            $criteria->select = 'TransactionNumber';   

            $criteria->limit = 10;            

            $criteria->order = 'TransactionNumber DESC';  

            $last = $this->find($criteria);

            if ($last) {   

                $nomorId = (int) substr($last->TransactionNumber,  6) + 1;

                $nomorId = date('ymd') . '' . $nomorId;

            } else { 

                $nomorId = date('ymd') . '-1';

            }

            $this->TransactionNumber = $nomorId;

        }

        return true;

    }



i want to get output = 15031200001, but i always get output 15031201

i need add right digit.

anybody can help me?

Use sprintf to padding number.




                $nomorId = date('ymd') . '' . sprintf("%06d", $nomorId);




thanks Fabrizio Caldarelli. :D .

when i create TransactionNumber, this number can’t return back number when changed date.

example today : 2015-03-12 when i generate the number to be 150312000001 until 150312000112

when date 2015-03-13 generate number is 150312000113, but i want to return back to number 1 again

150312000001 not 150312000113 how to fix it…

sorry my english is bad.

OK, you have to change condition in criteria to find only record for the current day.




protected function beforeValidate() {

        parent::beforeValidate();

        if ($this->isNewRecord) {

            $criteria = new CDbCriteria; 

            $criteria->select = 'TransactionNumber';   

            $criteria->limit = 10;            

            $criteria->condition = "LEFT( TransactionNumber, 6 ) = DATE_FORMAT(CURRENT_DATE, '%y%m%d')";

            $criteria->order = 'TransactionNumber DESC';  

            $last = $this->find($criteria);

            if ($last) {   

                $nomorId = (int) substr($last->TransactionNumber,  6) + 1;

                $nomorId = date('ymd') . '' . sprintf("%06d", $nomorId);


            } else { 

                $nomorId = date('ymd') . '' . sprintf("%06d", 1);

            }

            $this->TransactionNumber = $nomorId;

        }

        return true;

    }



thanks… but i get error

Error 500

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword ‘CURRENT_DATE’.

is not compatible CURRENT_DATE in SQL Server??

thank for your help.

Sorry, I thought that you are using MySQL.

With SQLServer condition should be:




LEFT( TransactionNumber, 6) =  CONVERT(VARCHAR(6),GETDATE(),6)



porgive me sir. i wanna ask again, when i used this code

i got error duplicate entry.




protected function beforeValidate() {

        parent::beforeValidate();

        if ($this->isNewRecord) {

            $criteria = new CDbCriteria; 

            $criteria->select = 'TransactionNumber';   

            $criteria->limit = 10;            

            $criteria->condition = 'LEFT( TransactionNumber, 5) =  CONVERT(VARCHAR(5),GETDATE(),5)';

            $criteria->order = 'TransactionNumber DESC';  

            $last = $this->find($criteria);

            if ($last) {   

                $nomorId = (int) substr($last->TransactionNumber,  5) + 1;

                $nomorId = date('ymd') . '' . sprintf("%05d", $nomorId);


            } else { 

                $nomorId = date('ymd') . '' . sprintf("%05d", 1);

            }

            $this->TransactionNumber = $nomorId;

        }

        return true;

    }



line code : $nomorId = (int) substr($last->TransactionNumber, 5) + 1; —> not working

but when i used this code




protected function beforeValidate() {

        parent::beforeValidate();

        if ($this->isNewRecord) {

            $criteria = new CDbCriteria;

            $criteria->select = 'TransactionNumber';

            $criteria->limit = 10;

            $criteria->order = 'TransactionNumber DESC';

            $last = $this->find($criteria);

            if ($last) {

                $nomorId = (int) substr($last->TransactionNumber, 5) + 1;

                $nomorId = date('ymd') . '' . sprintf("%05d", $nomorId);

            } else {

                $nomorId = date('ymd') . '-1';

            }

            $this->TransactionNumber = $nomorId;

        }

        return true;

    }



2nd code run, but can not return back when change date.