php维护从过去日期到当前日期的月、周、日的日期增量
我正在努力正确地提出这个问题。
我在名为 renew 的 mysql 表列中有一个 php time() 条目。我在列类型下的同一行中有第二个条目,它决定何时更新行,即:从更新列值(日期)开始每天或每周或每月。
该脚本的工作原理: 在此示例中,行 id 1;必须更新余额以反映每月预算。还必须更新续订日期,以反映更新发生后的下一个每月递增日期。
在行 id 2 中;这将每周进行一次。等等。
例如:
| id | renew | type | budget | balance |
|----| ---------- | ---------| ------ | ------- |
| 1 | 1611214417 | monthly | 10,000.00 | 3,000.00 |
| 2 | 1643614417 | weekly | 4,000.00 | 600,00 |
id 1 = 2021-01-21 07:33:37;
id 2 = 2022-01-31 07:33:37;
在行 id 1 中如何获取从今天开始的下一个更新日期?
//the time entry
$now = time():
$today = 28/02/2022
$updatetime = $table['renew'];
$updatetype = $table['type'];
我每周的表现如何: 从上面的 $updatetime 变量中,我将变量增加了一星期,直到它刚刚经过 $now 时间。
// what I've done for weekly and seems to be working
for($i = 0; $i < 1000000000; $i++ ){ //random high number for incrementing
if($updatetime < $now){
$updatetime = $updatetime + (86400 * 7); //add one week
} else {
return;
}
}
// the next update will now be the new $updatetime for weekly
我怎样才能每月做到这一点?一个月中没有固定的日期,续订日期可以是该月的 31 日或 2 月 29 日,然后当月只能有 30 天或 28 天。 这就是我的问题所在。
如果你能帮助我的话。
I'm struggling to formulate this question correctly.
I have a php time() entry in a mysql table column called renew. I have a second entry in the same row under the column type which decides when to update the row, namely: daily or weekly or monthly from the renew column value (date).
How this script works:
In this example on row id 1; the balance must be updated to reflect the budget monthly. The renew date must also be updated to reflect the next monthly incremented date after the update has taken place.
in row id 2; This will take place weekly. and so on.
for example:
| id | renew | type | budget | balance |
|----| ---------- | ---------| ------ | ------- |
| 1 | 1611214417 | monthly | 10,000.00 | 3,000.00 |
| 2 | 1643614417 | weekly | 4,000.00 | 600,00 |
id 1 = 2021-01-21 07:33:37;
id 2 = 2022-01-31 07:33:37;
in row id 1 how to get the next update date from today?
//the time entry
$now = time():
$today = 28/02/2022
$updatetime = $table['renew'];
$updatetype = $table['type'];
How i did the weekly:
from the $updatetime variable above, I incremented the variable with one week until it just passes the $now time.
// what I've done for weekly and seems to be working
for($i = 0; $i < 1000000000; $i++ ){ //random high number for incrementing
if($updatetime < $now){
$updatetime = $updatetime + (86400 * 7); //add one week
} else {
return;
}
}
// the next update will now be the new $updatetime for weekly
How can i do this for the monthly? there are no fixed days in a month, the renew date could fall on the 31 of the month or 29 of February and then the current month could only have 30 days or 28 days.
This is where my issue lies.
If you can assist me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 PHP DateTime 对象 操作起来更简单使用 相对格式 的日期时间
使用其他日期操作示例相对格式:
现场演示 现场演示没有时间重置
提示:
将
daily
、weekly
、monthly
转换为不定式day
、week
、month
这样您就可以通过$table['type']
直接在日期时间操作中使用它的值:TIP2*
使用 ISO 日期格式
Ymd
来简化编码。I would recommend to use PHP DateTime object that is much simpler to manipulate the date time using relative formats
Other examples of date manipulation using relative formats:
Live demo Live demo without time reset
TIP:
Convert your
daily
,weekly
,monthly
into infinitiveday
,week
,month
so you will be able to use its value directly using$table['type']
into the date time manipulation:TIP2*
Use ISO date format
Y-m-d
to simplify your codding.感谢 Ino 的回答,我是这样解决的:
Thanks to Ino's answers i worked it out like this: