php get the upcoming date from month of day

Solution:
You need to do the following:
If today is <= from the day you want, just add (day - today) days, else, add one month to the current day and subtract (today - day).
<?php
$day = 19;
echo nextOccurrence($day);
function nextOccurrence($day) {
$currentDay = date('d');
if ($day > $currentDay) {
// Get the timestamp of $day in this month
$date = strtotime('+' . ($day - $currentDay) . ' days');
} else {
// Get the timestamp of the current day in next month, and subtract the days difference
$date = strtotime('+1 month -' . ($currentDay - $day) . ' days');
}
return date('Y-m-d', $date);
}
?>
Answer
Solution:
Please try this code :
echo '<br>Today is :' . date('Y-m-d');
echo '<br>Next day is : ' . date('Y-m-d',strtotime("+1 day"));
Thanks....
Answer
Solution:
please refer to the below code:
function nextOccurrence($day) {
$today = date("d");
if ($today <= $day) {
return $retDate = date("d-M-Y", strtotime(date("Y-m-d") . '+1 month'));
} else {
return $retDate = date("d-M-Y", strtotime(date("Y-M-$d") . "+1 day"));
}
}
hope problem solved.
Answer
Solution:
Try this, use strtotime and add 1 month(if month is february then get last day of next month) and add +1 day.
function nextOccurrence($day) {
$date_d = date('d');
if ($date_d <= $day) {
if($date_d > 28)
{
$get_date = date("d-m-Y", strtotime("last day of next month"));
}
else
{
$get_date = date("d-m-Y", strtotime("+1 month"));
}
}
else
{
$stop_date = date('Y-m-d H:i:s', strtotime($get_date . ' +1 day'));
}
echo $stop_date;
}
Answer
Solution:
To manipulate Date, please use DateTime Object.
- Step1: Initialize the date
- Step2: Create an interval of 1 Day.
- Step3: Add this interval to your Date
- Step4: Return date as a string.
It works even if your are the Feb 28th, 2008.
<?php
//Step 1
$date = new DateTime('2008-02-29'); //or new DateTime(date('Y-m-d')) for today
//Step2
$oneDay = new DateInterval('P1D')
//Step3
$date->add($oneDay);
//Step4 will return 1 (March 1st)
echo $date->format('d') . "\n";
To find the next occurence :
$nextOccurence = new DateTime();
while ($day != $nextOccurence->format('d')){
$nextOccurence->add($oneDay);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xmlhttprequest error flutter
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.