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.
CodePudding user response:
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!
CodePudding user response:
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