php维护从过去日期到当前日期的月、周、日的日期增量

发布于 2025-01-10 12:49:49 字数 1260 浏览 0 评论 0原文

我正在努力正确地提出这个问题。

我在名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2025-01-17 12:49:49

我建议使用 PHP DateTime 对象 操作起来更简单使用 相对格式 的日期时间

<?php

$today = new DateTime();

$updatetime = '19/01/2021'; // $table['renew']; //19/01/2021
$updatetype = 'weekly'; //$table['type']; //daily || weekly || monthly
  
$updatetime = (new DateTime())->createFromFormat('d/m/Y', updatetime);

// what I've done for weekly and seems to be working
 if($updatetime < $today){
    // $updatetime = $today; // reset to today if needed
    $updatetime->modify('next week'); // add one week
 } else {
    return;
 }
 
echo 'Next time of weekly update '. $updatetime->format('Y-m-d') . PHP_EOL . PHP_EOL;

// Output: Next time of weekly update 2022-03-07
// without time reset: Next time of weekly update 2021-01-25

使用其他日期操作示例相对格式:

<?php
$today = '2022-02-28';

$t = new DateTime($today);

// print the source date time
echo $t->format('Y-m-d') . PHP_EOL;

// examples of continuously manipulate the date time origin
echo $t->modify('next week')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next month')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next year')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next sunday')->format('Y-m-d') . PHP_EOL;
/*
Output:
2022-02-28
2022-03-07
2022-04-07
2023-04-07
2023-04-09
*/

现场演示 现场演示没有时间重置

提示
dailyweeklymonthly转换为不定式dayweekmonth 这样您就可以通过 $table['type'] 直接在日期时间操作中使用它的值:

<?php
$updatetype = 'week'; //$table['type']; //day || week || month
$updatetime->modify('next ' . $table['type']); // add one xxx dynamically!

TIP2*
使用 ISO 日期格式 Ymd 来简化编码。

$updatetime = '2021-01-19'; //'19/01/2021'
$updatetime = new DateTime($updatetime); // That's it!

I would recommend to use PHP DateTime object that is much simpler to manipulate the date time using relative formats

<?php

$today = new DateTime();

$updatetime = '19/01/2021'; // $table['renew']; //19/01/2021
$updatetype = 'weekly'; //$table['type']; //daily || weekly || monthly
  
$updatetime = (new DateTime())->createFromFormat('d/m/Y', updatetime);

// what I've done for weekly and seems to be working
 if($updatetime < $today){
    // $updatetime = $today; // reset to today if needed
    $updatetime->modify('next week'); // add one week
 } else {
    return;
 }
 
echo 'Next time of weekly update '. $updatetime->format('Y-m-d') . PHP_EOL . PHP_EOL;

// Output: Next time of weekly update 2022-03-07
// without time reset: Next time of weekly update 2021-01-25

Other examples of date manipulation using relative formats:

<?php
$today = '2022-02-28';

$t = new DateTime($today);

// print the source date time
echo $t->format('Y-m-d') . PHP_EOL;

// examples of continuously manipulate the date time origin
echo $t->modify('next week')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next month')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next year')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next sunday')->format('Y-m-d') . PHP_EOL;
/*
Output:
2022-02-28
2022-03-07
2022-04-07
2023-04-07
2023-04-09
*/

Live demo Live demo without time reset

TIP:
Convert your daily, weekly, monthly into infinitive day, week, month so you will be able to use its value directly using $table['type'] into the date time manipulation:

<?php
$updatetype = 'week'; //$table['type']; //day || week || month
$updatetime->modify('next ' . $table['type']); // add one xxx dynamically!

TIP2*
Use ISO date format Y-m-d to simplify your codding.

$updatetime = '2021-01-19'; //'19/01/2021'
$updatetime = new DateTime($updatetime); // That's it!
滥情哥ㄟ 2025-01-17 12:49:49

感谢 Ino 的回答,我是这样解决的:

 $today = time(); // can use DateTime() too.
 $updatetime = '1611214417'; // $table['renew']; //19/01/2021
 $updatetype = 'weekly'; //$table['type']; //daily || weekly || monthly
 
 echo $updatetime.'<br>';

  for($i = 0; $i < 100000; $i++){
  if($updatetime < $today){
  $updatetime = strtotime("+1 week", $updatetime); // can add week month day
  } else {
  break;
  }
  }

  echo $updatetime.'<br>'; // just for comparison

  echo date('Y-m-d', $updatetime); // 2022-03-03 works out perfectly 

Thanks to Ino's answers i worked it out like this:

 $today = time(); // can use DateTime() too.
 $updatetime = '1611214417'; // $table['renew']; //19/01/2021
 $updatetype = 'weekly'; //$table['type']; //daily || weekly || monthly
 
 echo $updatetime.'<br>';

  for($i = 0; $i < 100000; $i++){
  if($updatetime < $today){
  $updatetime = strtotime("+1 week", $updatetime); // can add week month day
  } else {
  break;
  }
  }

  echo $updatetime.'<br>'; // just for comparison

  echo date('Y-m-d', $updatetime); // 2022-03-03 works out perfectly 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文