Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kennybjr87's avatar

Month changing

I'm creating a rental property project with laravel to practice more advanced techniques what I'm stuck on is this. When creating a new renter you are able to select the due date, I'm trying to figure out how I can make a process automatic once the month has changed the due date column in the database should update to the new month so let's say rent is due 12/5/2018 how could I go about getting the new month change and updating to 1/5/2018?

0 likes
8 replies
tykus's avatar
tykus
Best Answer
Level 104

Carbon has several handy methods for adding and subtracting time:

$dueDate = '12/5/2018';
$newDueDate = Carbon::parse($dueDate)->addMonth();

EDIT: snap @jdunsmore !

2 likes
kennybjr87's avatar

Now my question is how do I "listen" for a month to pass?

jdunsmore's avatar

You could run a cron on the server.

To do so you should look at the docs for scheduling.

You edit your kernel.php file that is contained in the app folder then console folder.

(Can't link scheduling docs as I'm on my phone atm)

An do something like if carbon::now() >= $newrentdate then do whatever you require

gregrobson's avatar

@kennybjr87, a note of caution on the answer by @tykus

Blindly using addMonth() may have undesired consequences if you are looking to do something monthly like take rent (I'm guessing from your scenario).

// From the docs http://carbon.nesbot.com/docs/#api-addsub
$dt = Carbon::create(2012, 1, 31, 0);
echo $dt->toDateTimeString();            // 2012-01-31 00:00:00

echo $dt->addMonth();                    // Results in 2017-03-03 00:00:00
// Note that 1st January jumped to 3rd March, no February payment taken!

I've looked and this isn't documented on the docs website, but is in the source: addMonthsNoOverflow($value)

You can see from the source that if adding a month results in the day of the month changing (in this case 31st to the 3rd) it will back up to the last day of the previous month. e.g. 1st January will shift to 28th February (non-leap years)... May 31st would shift to June 30th etc.

If this used for taking a payment or similar then I've seen some applications that take payments from your bank only allow you to choose days of the month between 1 and 28 for payment processing. Therefore the day of the month never changes and you are saved a whole heap of bother having to deal with the day of the month changing!

If only our ancestors had made the calendar metric! :D

Cronix's avatar

I'd echo what @gregrobson stated

only allow you to choose days of the month between 1 and 28 for payment processing

You will save yourself a ton of headache.

I'd just store the day (int) of the month that the payment is due (10, 28, etc) in the database, not the full date.

Then, I'd run a daily() scheduled task (runs once each day at midnight) and just grab all payments that are due on that day:

$today = date('j');  // 10, 28, etc
$paymentsDueToday = Payments::where('dayDueColumn', $today)->get();
gregrobson's avatar

Building on @Cronix's suggestion, have some way to track when a payment was last processed for a given record. If processing fails entirely or mid-way it's good to be able to trace back who has/hasn't been processed, then you have options for re-running the previous day(s) transactions.

gregrobson's avatar

Sounds like a really good "real world" scenario for practising your coding! :-)

Please or to participate in this conversation.