I want same day of month for each of the months that fall between start date and end date. Its just that if the month of the day is not valid for a particular month, you want last day of that month. is there any script?. What is have done is.
$startdate='2010-01-30';
$enddate='2011-01-30';
while ($startdate <= $enddate)
{
echo date('Y-m-d', $startdate ) . "\n";
$startdate = strtotime('+1 month', $startdate);/// for case of feb 28 days last date it should disply as it skips it
}
Expected out put:
For input $startdate='2012-01-30'; $enddate='2013-12-30' Result should be like this ==>
_2012-12-30_
2013-01-30
**2013-02-28**
2013-03-30
2013-04-30
2013-05-30
2013-06-30
2013-07-30
2013-08-30
2013-09-30
2013-10-30
2013-11-30
_2013-12-30_
Loop using for and adding $i months to the start date instead of constantly adding one month to the running value. This way it won't jump to 28th (or 29th) day because of february starting from march.
Related
I want to get the previous months date range with PHP. I have tried below function to get last month's date range.
$last_month_start_date = date('Y-m-d',strtotime('first day of last month'));
$last_month_end_date = date('Y-m-d',strtotime('last day of last month'));
echo $last_month_start_date.' - '.$last_month_end_date;
Output: 2019-09-01 - 2019-09-30
but how can I get previous month's date range with PHP?
can anybody help me with this?
To get Last to Last Month: If current month is October then below output gives first and last date of August
$last_to_last_start_date = $last_month_start_date = date('Y-m-d',strtotime('first day of last month -1 month'));
// outout 2019-08-01
$last_to_last_end_date = $last_month_end_date = date('Y-m-d',strtotime('last day of last month -1 month'));
// outout 2019-08-31
echo $last_to_last_start_date .' - '.$last_to_last_end_date ;
//Output : 2019-08-01 - 2019-08-31
Create a date-time object that is two months behind, then use a modifier of first/last day of this month. The this month modifier works on the month of the object, so if the object's date is in August, it will look for the first and last days of August.
This means that you can provide any value for -2 months (like February, -1 month) and it will get the start and end-dates for that month.
$start = new DateTime("-2 months");
$end = clone $start;
$start->modify("first day of this month");
$end->modify("last day of this month");
echo $start->format("Y-m-d")." - ".$end->format("Y-m-d");
In October 2019, this outputs,
2019-08-01 - 2019-08-31
Live demo at https://3v4l.org/udGtE
Hello I am looking to do a date calculation function. I have a first date for example 2018-06-29 and I would like to add 30 days to this date, if by adding 30 days I fall on a Sunday I would like to withdraw -1 day so fall on a Saturday, the date recover from the calculation will be 2018-07-28 and so on ....
If the date falls in the month of July, we will postpone for the month of June, and if we fall in August, we will postpone the month of September.
Currently I get the first date like this
Array
(
[date_delivery] => 2018-06-27
)
And I prepare the calculation function
public function calculDate($dateDelivery)
{
ddd($dateDelivery);
}
Thank you for your help.
function calcDate($date) {
$newDate = strtotime('+1 month',$date);
$day = day('D',$newDate);
if ($day == 'Sun') $newDate = strtotime('-1 day', $newDate);
}
I need to fetch previous month from the current month passed. Tried below code and it does work for the month having 30 days but does not work for specific months having 31 days viz. March, May, July, October and December
Note: The question may sound repeated, but please read it completely till the end. You can check the same issue by changing the system and testing below code against it. I need the previous month output in format Jul
For Date: 30-Jul output is
Previous Month-Jun
Current Month-Jul
For Date: 31-Jul output is
Previous Month-Jul
Current Month-Jul
$prev_month = date('M', strtotime("last month"));
echo 'Previous Month--'.$prev_month;
echo 'Current Month--'.date('M');
Also tried echo date('M', strtotime("-1 Months")); but it outputs the same as above.
If current month is (August) with 31 days and so previous month is (July) with 31 days then it works and shows correct Previous Month i.e. July, but it does not work if current month has 31 days and previous month has 30 or lesser days.
How should I go about it to fetch correct previous month on the basis of current month ?
You simply try as
echo "Previous Month".date('M',strtotime('first day of last month'));
and for specific date you can simply use
echo date('M',strtotime('first day of last month',strtotime('30-Jun')));//May
You should pass in the date from the start of the month.
$startDate = date('Y-m-1');
$prevMonth = date('M', strtotime("last month", strtotime($startDate)));
see
PHP date() and strtotime() return wrong months on 31st
I need to check if the end of the month is within 10 days. If not, the date should be the first day of the following month. If the end of the month is within the next ten days, then I need to display first day of the month following next month.
Here is my code:
$tenDays = date('Y-m-d', strtotime('+10 days'));
$firstDayNextMonth = date('Y-m-d', strtotime('$tenDays, first day of next month'));
echo"$tenDays";
echo "<br>";
echo "$firstDayNextMonth";
The output is:
2015-08-01
1970-01-01
Based on the date today 2015-07-22 the desired outcome for the second line should be 2015-09-01, not 1970-01-01.
Stop using strtotime() for date math. That's not what it is there for. Use DateTime().
$firstDayNextMonth = (new DateTime('+10 days'))->modify('first day of next month')->format('Y-m-d');
Demo
Use 00 as the day-of-month:
$foo = new DateTime('2015-03-00');
$foo->format('r');
As you can see, the 0th of March, is the last day of February. So a simple $foo->format('d') will get you that last-day-of-month.
I'm aware of the vast amount of date questions on SO but I am stumped at this one.
I need to run some reports and in order to do so I need to work out some dates.
The report will run on a Monday and I need to work out the dates of the Sunday the week before and the following Saturday.
e.g. The report will be run on Monday the 28th May and the dates I need are Sunday the 20th May and Saturday the 26th May.
I then need the same values but for the previous week, so Sunday the 13th May and Saturday the 19th May.
I think this part will be fine as it's just a case of getting the current date of the Monday when the report is run and and manipulating it from there.
THEN finally, the part I can't work out is the first week I mentioned, I need the corresponding dates from last year, so the dates of the Sunday and Saturday from the same week number in the previous year.
I know the date function can give you the week number but can't see how to work out the dates of the Sunday of that week and the previous Saturday based on that.
This is what I have, forgive the OTT variable names:
$today = date('Y/m/d');
// reports run on a Monday and use last Sunday and the following Saturday's date so get yesterday's date first
$yesterday = strtotime ('-1 day', strtotime($today));
$yesterday = date ('Y/m/d', $yesterday);
// start of week (last Sunday)
$start_of_last_week = strtotime ('-1 week', strtotime($yesterday));
$start_of_last_week = date ('Y/m/d', $start_of_last_week);
// end of last week (the following Saturday)
$end_of_last_week = strtotime ('+ 6 days', strtotime($start_of_last_week));
$end_of_last_week = date ('Y/m/d', $end_of_last_week;
// start of previous week
$start_of_previous_week = strtotime ('-1 week', strtotime($start_of_last_week));
$start_of_previous_week = date ('Y/m/d', $start_of_previous_week);
// end of previous week
$end_of_previous_week = strtotime ('+ 6 days', strtotime($start_of_previous_week));
$end_of_previous_week = date ('Y/m/d', previous;
// the start of the same week last year
$start_of_last_week_last_year = strtotime ('-1 year', strtotime($start_of_last_week ));
But the above isn't right so not sure what to do next.
Any help is much appreciated.
You can find out the date of the Monday of a particular year and week number like this
date( 'Y-m-d', strtotime( '2012W21' )); //2011-05-23
Compound Date Formats