How to find first day of the next month and remaining days till this date with PHP
Create a timestamp for 00:00 on the first day of next month:
$firstDayNextMonth = strtotime('first day of next month');
The number of days til that date is the number of seconds between now and then divided by (24 * 60 * 60).
$daysTilNextMonth = ($firstDayNextMonth - time()) / (24 * 3600);
How to find the first day of NEXT month - PHP
Your code to calculate the first day of next month is not quite right. The answer you have linked looks different to the code you have used.
strtotime('first day of next month')
calculates the first day of the current month and will bear no relation to the start date.
You could use a DateTime
object and figure it out from there:
$start_pay_date = new DateTime($start_subcription);
$first_day_next_month = $start_pay_date->modify("first day of next month");
$first_day_next_month
would then represent the first day of the month after your $start_pay_date
, you can use any of the DateTime
methods to then format it how you want (e.g. $first_day_next_month->format("Y-m-d")
)
Round' up a date to the beginning of the next month
You can use DateTime like:
$dateTime = new DateTime('2017-03-17');
$dateTime->modify('first day of next month');
echo $dateTime->format('Y-m-d');
Get 1st day of next month
This seems like a duplicate or similar post to this for the next month
but for the next month something like this if you want to use strtotime
date("m/l/Y", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00'));
this will return
12/Saturday/2012 if you want just the days name just set the date format to l (lower case L)
date("l", strtotime(date('m', strtotime('+1 month')).'/01/'.date('Y').' 00:00:00'));
How to find first day of the next month and remaining days till this date with PHP
or this
In PHP, is there an easy way to get the first and last date of a month?
How to find the last day of the month from date?
t
returns the number of days in the month of a given date (see the docs for date
):
$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));
PHP add month to date on a date with day of month 31
I think this should work -
$time = strtotime("2015-05-31");
echo $final = date("Y-m-d", strtotime("first day of next month", $time));
Calculating remaining days in a month
You can use strtotime
and date
.
For the format of date
you can use the following:
't'
Number of days in the given month (28 through 31)
'j'
Day of the month without leading zeros (1 to 31)
<?php
$timestamp = strtotime('2014-10-03');
$daysRemaining = (int)date('t', $timestamp) - (int)date('j', $timestamp);
var_dump($daysRemaining); // int(28)
DEMO
Edit: Apparently you want to list the remaining days in the month:
<?php
$timestamp = strtotime('2014-10-03');
$yearMonth = date('Y-m-', $timestamp);
$daysInMonth = (int)date('t', $timestamp);
for ($i = (int)date('j', $timestamp); $i <= $daysInMonth; $i++) {
$dateString = date('l \t\h\e jS \o\f F', strtotime($yearMonth . $i));
var_dump($dateString);
}
/*
string(25) "Friday the 3rd of October"
string(27) "Saturday the 4th of October"
string(25) "Sunday the 5th of October"
string(25) "Monday the 6th of October"
string(26) "Tuesday the 7th of October"
string(28) "Wednesday the 8th of October"
string(27) "Thursday the 9th of October"
string(26) "Friday the 10th of October"
string(28) "Saturday the 11th of October"
string(26) "Sunday the 12th of October"
string(26) "Monday the 13th of October"
string(27) "Tuesday the 14th of October"
string(29) "Wednesday the 15th of October"
string(28) "Thursday the 16th of October"
string(26) "Friday the 17th of October"
string(28) "Saturday the 18th of October"
string(26) "Sunday the 19th of October"
string(26) "Monday the 20th of October"
string(27) "Tuesday the 21st of October"
string(29) "Wednesday the 22nd of October"
string(28) "Thursday the 23rd of October"
string(26) "Friday the 24th of October"
string(28) "Saturday the 25th of October"
string(26) "Sunday the 26th of October"
string(26) "Monday the 27th of October"
string(27) "Tuesday the 28th of October"
string(29) "Wednesday the 29th of October"
string(28) "Thursday the 30th of October"
string(28) "Friday the 31st of October"
*/
DEMO
Round' up a date to the beginning of the next month
You can use DateTime like:
$dateTime = new DateTime('2017-03-17');
$dateTime->modify('first day of next month');
echo $dateTime->format('Y-m-d');
PHP | Calculate remaining days but taking leap years into account
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
Related Topics
Correct Format for Strings/Numbers Beginning with Zero
Change Woocommerce Default Password Security Level
PHP - Cannot Use a Scalar as an Array Warning
How to Execute a Shell Script in PHP
Codeigniter: How to Do a Select (Distinct Fieldname) MySQL Query
Curl File Uploads Not Working Anymore After Upgrade from PHP 5.5 to 5.6
How to Change the Array Key to Start from 1 Instead of 0
How to Convert a String to an Array in PHP
Verify Password Hash in Nodejs Which Was Generated in PHP
How to Check for Special Characters PHP
Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted (Tried to Allocate 71 Bytes)
How to Redefine a Type Hint to a Descendant Class When Extending an Abstract Class
Reading from Comma or Tab Delimited Text File
What Is the Safest Way of Passing Arguments from Server-Side PHP to Client-Side JavaScript
Does Static Method in PHP Have Any Difference with Non-Static Method