5 ways to generate unique order numbers in php
This article mainly introduces the method of generating a unique order number in php, which has a certain reference value. Now I share it with you. Friends in need can refer to it
php generates a unique order number 5 methods
The first one
private function doCreateOrderNumber($time){ $i=1; $dd = date('Ymd',$time); $aa = 'OH'.$dd; $res = $this->orderModel->query("select sn from sr_order_list where sn like '$aa%' order by id limit 1"); if(!isset($res[0]['sn'])){ $i = 1; }else{ $i = (int)substr($res[0]['sn'],9,10) + 1; } while(true){ $nsn = 'OH'.$dd.$i; $exist = $this->orderModel->query("select id from sr_order_list where sn = '$nsn' "); if($exist){ $i++; }else{ return $nsn; } } }
The second one
$osn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); echo $osn; //2018070462577
The third one
$osn = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); echo $osn; //2018070499495653
The fourth one
$order_id_main = date('YmdHis') . rand(10000000,99999999); $order_id_len = strlen($order_id_main); $order_id_sum = 0; for($i=0; $i<$order_id_len; $i++){ $order_id_sum += (int)(substr($order_id_main,$i,1)); } $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT); echo $osn; //201807041332258742313727
Fifth Type
$code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $osn = $code[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99)); echo $osn; //H704764673624352
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
php code to implement WeChat payment
##Introduction to installing xhprof for performance analysis in PHP 7.1
The above is the detailed content of 5 ways to generate unique order numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Alipay PHP...

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
