Order Number Generator
Generates readable unique order numbers separate from database IDs.
Function signature
ogCreateOrderNumber(input = array(), options = array())
Categories
- Database Integrity
Parameters
inputStructured workflow input array documented by this helper.optionsOptional documented policy controls for the helper.Return value
Public-safe status string returned by the function for controller branching or logging.
- success
- message
- data
Compatibility
Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.
Minimum PHP version: 7.4
Security notes
Use caller-owned allowlists and context-specific escaping; validate file paths, routes, email tokens, cart totals, discount rules, and tax-region rules before production use.
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Generates readable unique order numbers separate from database IDs.
*
* Primary use case: Ecommerce checkout.
* Typical inputs: prefix, date, sequence/random policy.
* Typical output: order number.
*
* Implementation note: Check uniqueness before final assignment.
*
* @param array $input Structured input values for this helper contract.
* @param array $options Optional policy and formatting controls.
* @return array Structured result data with success, message, and data keys.
*/
function ogCreateOrderNumber($input = array(), $options = array()) {
$result = array(
'success' => false,
'message' => '',
'data' => array()
);
if (!is_array($input)) {
$result['message'] = 'Input must be an array.';
return $result;
}
if (!is_array($options)) {
$options = array();
}
$prefix = 'OG';
if (!empty($input['prefix'])) {
$prefix = strtoupper(preg_replace('/[^a-zA-Z0-9]/', '', (string)$input['prefix']));
}
$sequence = 0;
if (!empty($input['sequence'])) {
$sequence = (int)$input['sequence'];
}
$timestamp = time();
if (!empty($input['timestamp'])) {
$timestamp = (int)$input['timestamp'];
}
if (empty($prefix)) {
$prefix = 'OG';
}
if ($sequence < 1) {
$sequence = random_int(1000, 9999);
}
$date_part = gmdate('Ymd', $timestamp);
$sequence_part = str_pad((string)$sequence, 6, '0', STR_PAD_LEFT);
$check = strtoupper(substr(hash('crc32b', $prefix . $date_part . $sequence_part), 0, 4));
$order_number = $prefix . '-' . $date_part . '-' . $sequence_part . '-' . $check;
$result['success'] = true;
$result['message'] = 'Order number created.';
$result['data'] = array('order_number' => $order_number);
return $result;
}