Explorer Explorer - 2 months ago 13
MySQL Question

auto increment receipt number and year

currently my project receipt number is auto incrementing. eg: 1, 2, 3, ...n
I want to prefix the financial year to differentiate the recept number of every year.
like, 2016-17/1 then 2016-17/2 and so on.
and year should be change according to financial year (1st april to 31 march assume as one year).
once the date is 1st april then receipt number should be like 2017-18/1 then 2017-18/2 and so on.
So, my question is..Is there any code through which i can have the recept no prefix with year.
thank you so much in advance.

<div style="float:left">
<label> Receipt No:</label>

<input type="text" class="form-control" style="width: 60% !important; color:#999999;" name="receipt_no" value="<?php
$query = mysql_query("SELECT `receipt_no` FROM tsurphus");
if(mysql_num_rows($query)>0){
$data = mysql_query("SELECT max(receipt_no)+1 FROM tsurphus");
$res = mysql_fetch_row($data);
echo $res[0];
echo $_POST['$res'];
}
else {
echo 1;
}
?>" readonly>

</div>

Answer

So from 1/4 to 31/12 you need <current_year> - <next_year> and from 1/1 - 31/3 <last_year> - <current_year>

<div style="float:left">
<label> Receipt No:</label>

<input type="text" class="form-control" style="width: 60% !important; color:#999999;" name="receipt_no" value="<?php


$curr_month = date('m');

if($curr_month >= 4 && $curr_month <= 12){
    $year = date('Y') . '-' . date('y', strtotime("+1 year"));
} else {
    $year = date("Y",strtotime("-1 year")) . '-' . date("y");;
}
echo $year.'/';

$query = mysql_query("SELECT `receipt_no` FROM tsurphus");
if(mysql_num_rows($query)>0){
    $data = mysql_query("SELECT max(receipt_no)+1 FROM tsurphus");
    $res = mysql_fetch_row($data);
    echo $res[0];
    echo $_POST['$res'];
}
else {
    echo 1;
}
?>" readonly>

but the receipt number is continuing even when the financial get change. instead of continuing the receipt number it should reset to 1

You can try to get the max counter for the current year and use it. Something like

$query = mysql_query("SELECT `receipt_no` FROM tsurphus WHERE recipt_no LIKE '".$year."%'");
Comments