Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Crazylife's avatar

How to generate a unique id based on own format?

I have a table as shown below

| id         | id_p       | doc_no       |
|:-----------|------------:|:------------:|
| 1           | 1             |     D11710001
| 2          | 2            |     D21710001
| 3          | 3            |     D31710001
| 4          | 4           |     D41710001
| 5          | 5            |     D51710001
| 6          | 1            |     D11710002

I want to generate an unique id based on the id given. For example, when i have item to be stored in this table, it will generate an unique id based on the id of the table.

Note: The id in this table is a foreign key. The doc no can be modified by user into their own format manually. The doc no only will be generated automatically when user does not input a value to the doc no field.

The id format - P 'id' 'month' 'day' 001(auto increment)

How can i query this to generate unique id during storing data?

0 likes
5 replies
yearulislam's avatar

@meenctg but this package always start from 1. if i want to start from 500/666, so how to do it?

Please or to participate in this conversation.