-3

I would like to create a serial number the following way.

The serial number should go back to 01 with the start of each month.

$stockno=dofetch(doquery("Select count(id) from vehicle",$dblink));
$stock_no= date("Ymd")."-".$stockno=$stockno["count(id)"]+1;

output is

20150424-01

20150424-02

20150424-03

...

20150430-120

20150501-121

20150501-122

20150502-122

but need Result below format, when new month start serials will be reset

20150424-01

20150424-02

20150424-03

...

20150430-120

20150501-01

20150501-02

20150502-03

new month start again reset to 1

20150601-01

etc.

Behlum Noman
  • 421
  • 4
  • 5
  • 1
    use date column in vehicle table after which you will be able to get count on date basis and if the month is over you will get a count zero which will be incremented accordingly – lakshman Apr 24 '15 at 03:38
  • Just use a loop and have a variable storing the previous month. if month changes, intialize the count. – Reuben L. Apr 24 '15 at 03:38
  • 4
    ‐1 do-my-work-for-me – Sharky Apr 24 '15 at 06:40

1 Answers1

0

Here you go,

you just need to get last inserted serial value.

$dateString = "20150424-03"; // get Last entry's date from database. ( date("Ymd"). Ex. 20150424-03
$dtData = split('-',$dateString); // $dtData[0] = date part, $dtData[1] = Prefix part
$month1 = date("m",strtotime($dtData[0])); //Get the month of retrived date

//$date2 = date("Ymd"); // Get current date
// OR 
$date2 = date("Ymd", strtotime("+1 day", strtotime($dtData[0]))); // OR Increment last-date by one day
$month2 = date("m",strtotime($date2));  // Get updated date's  month

// now calculate month difference between dates. if months are same, prefix will be increased else prefix will be reset to 1
$prefix = (($month2-$month1) == 0) ? str_pad(++$dtData[1], 2, '0', STR_PAD_LEFT) : str_pad(1, 2, '0', STR_PAD_LEFT);
echo $date2. '-'. $prefix;

Simple and doesn't need any changes in database

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66