亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何生成YYYYMM0001這個格式的數字?

如何生成YYYYMM0001這個格式的數字?

PHP
qq_花開花謝_0 2023-09-22 16:47:47
我需要生成一組由當前年份、當前月份和零前導的增量 4 位數字組成的數字。有條件:最后 4 位數字是從數據庫中的最后一條記錄開始遞增的。例子: 2020080023 -> last database record 2020080024 -> so this is will be the next record在月初,最后 4 位數字重置為 0001。例子: 2020080023 -> last database record on end of month 2020090001 -> so this is will be the next record這是我到目前為止所擁有的:$currentYear = date('Y');$currentMonth = date('m');$last4Digits = '0001';$formatedNumber = $currentYear.$currentMonth.$last4Digits;問題如何增加最后一條記錄的最后 4 位數字以及如何在月初重置為 0001?
查看完整描述

2 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

該解決方案需要最后格式化的 ID 和一些邏輯。如果最后一個ID是當前月份,則只需加1。如果不是,則新月份從1開始。


function getId($lastId){

  $curYm = (int)date('Ym');

  $lastYm = (int)($lastId/10000);

  if($lastYm == $curYm) return ++$lastId;

  return $curYm * 10000 + 1;

}

例子:


$id = 2020080007;

$nextId = getId($id); //int(2020080008)


$id = 2020070127;

$nextId = getId($id); // int(2020080001)


查看完整回答
反對 回復 2023-09-22
?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

獲取最后一個記錄月份并檢查 $currentMonth 和 Lastrecords 月份是否相同,如果相同則增加當前 $last4Digits 否則設置 $last4Digits = '0001'


$currentYear = date('Y');

$currentMonth = date('m');


// last record from db

$db_last4Digits = //data from db;

$db_last4Digits_month = substr($db_last4Digits, 4, 2);

$db_last4Digits_month = substr($db_last4Digits, 6);



if($db_last4Digits_month == $currentMonth){

$last4Digits = '0001'; // set to 0001 if current month is not equal to last month

}else{

$last4Digits = substr($db_last4Digits, -4, 4); //get last 4 numbers

$last4Digits += $last4Digits ;

}



$formatedNumber = $currentYear.$currentMonth.$last4Digits;


查看完整回答
反對 回復 2023-09-22
  • 2 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號