gpt4 book ai didi

php - 我正在尝试自动增加发票号码

转载 作者:行者123 更新时间:2023-11-29 15:29:41 24 4
gpt4 key购买 nike

大家好,我正在尝试自动增加发票号码,问题是当涉及到 2019-10 时,会自动返回到 2019-01,她没有完成增加,有人可以帮助我吗

<?php  

$value2='';
//Query to fetch last inserted invoice number
$query = "SELECT numfacturation from facturation order by numfacturation DESC LIMIT 1";
$stmt = $con->query($query);
if(mysqli_num_rows($stmt) > 0) {
if ($row = mysqli_fetch_assoc($stmt)) {
$value2 = $row['numfacturation'];
$value2 = substr($value2, 6, 13);//separating numeric part
$value2 = $value2 + 1;//Incrementing numeric part
$value2 = "2019-" . sprintf('%02s', $value2);//concatenating incremented value
$value = $value2;
}
}
else {
$value2 = "2019-01";
$value = $value2;
}

?>

最佳答案

请参阅 substr() 手册;

substr ( string $string , int $start [, int $length ] ) : string

Returns the portion of string specified by the start and length parameters.

string

The input string. Must be one character or longer.

start

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

请注意“从零开始计数”部分。

考虑两个例子:

$value2 = '2019-XY';
$value2 = substr($value2, 6, 13);
var_dump($value2);

结果:字符串(1)“Y”

$value2 = '2019-XY';
$value2 = substr($value2, 5, 13);
var_dump($value2);

结果:字符串(2)“XY”

所以你需要计算从位置0开始的字符。

string:   2019-10
position: 0123456

如您所见,您的号码从位置 5 开始(而不是 6)。

另请注意,您使用 13 作为长度。但如果您的号码始终是两位数长,您应该使用:

$value2 = substr($value2, 5, 2);

关于php - 我正在尝试自动增加发票号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com