hi,
The best option would be to let the database do it for you.(i am assuming that you will store the order number in a dbms and this would be the primary key).
That is generate the order number in your script,store it in a table .
Everytime you need to add another order number just select the most recent one ,increment it and store the new one. This way you can ensure that no two order numbers are same.(this is for the situation mentioned in your question)
Alternatively, you can set the order number as primary key with auto increment option and set the default value(start value) to any number you want. Alongwith this you can also have a date time stamp field which will tell you when the order was recorded.
i hope this helps! cheers