我想通过以下方式创建一个序列号。
序列号应在每月开始时返回01。
$stockno=dofetch(doquery("Select count(id) from vehicle",$dblink));
$stock_no= date("Ymd")."-".$stockno=$stockno["count(id)"]+1;
输出
20150424-01
20150424-02
20150424-03
...
20150430-120
20150501-121
20150501-122
20150502-122
但需要以下格式的结果,当新月启动序列将被重置时
20150424-01
20150424-02
20150424-03
...
20150430-120
20150501-01
20150501-02
20150502-03
新月开始再次重置为1
20150601-01
等
答案 0 :(得分:0)
你走了,
您只需要获取最后插入的序列值。
$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;
简单,不需要对数据库进行任何更改