Reputation:
small problem to calculate the remaining days of the month starting today just I write:
$today = date('Y/m/d');
$timestamp = strtotime($today);
$daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
echo $daysRemaining;
and I get the remaining days
to do a test I entered a static date for the month of February
$timestamp = strtotime('2020-02-01');
$daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
echo $daysRemaining;
the question is here how do I calculate the remaining days in the month taking into account leap years, for example in February 2020 it will have 29 days and in this way I get out of it that remain 28
Upvotes: 2
Views: 323
Reputation: 12374
Stop using the functions and start using the DateTime class!!! This code should explain itself.
<?php
$x = new DateTime('2020-02-17'); // create your date
$y = clone $x; // copy the date
$y->modify('last day of this month'); // alter the copy to the last day
echo $x->format('d') . "\n"; // show the day of the first date
echo $y->format('d') . "\n"; // show the day of the second date
echo $y->format('d') - $x->format('d'); // show the difference between the two
Output:
17
29
12
Check it here https://3v4l.org/8ZhOb
Check the DateTime class docs here https://www.php.net/manual/en/class.datetime.php
Upvotes: 3