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
Related
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 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.
Hy, I have in database number of the week date ("W") and I want to display week interval like 28 Jan -> 3 Feb in this format, and I don't know if it's possible. Can you help?
Thanks!
Try this
$year = 2013;
$week_no = 6;
$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
$week_end = clone $week_start;
$week_end = $week_end->add(new DateInterval("P1W"));
echo $week_start->format('d-M-Y') . " - ".$week_end->format('d-M-Y');
Transform your intervals into timestamps.
If its not the first day of the week get the first day of that week with strtotime "last sunday" (or monday) for the first date.
Do the same for the second date this time geting the last day of the week with "next saturday" (or sunday);
Get both dates W and make a mysql comparison between the weeks.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Grab current first and last day in week in php
I am trying to get the first and last day of the week, month, quarter, and year for a given day.
For instance, for day 2013-01-16, the first and last days would be:
Week - It would be 2013-01-13 and 2013-01-19 (assuming Sunday to Saturday)
Month - It would be 2013-01-01 and 2013-01-31
Quarter - It would be 2013-01-01 and 2013-03-31
Year - It would be 2013-01-01 and 2013-12-31
My purpose is to include them in a WHERE myDate BETWEEN 2013-01-13 AND 2013-01-19.
Both a standard MYSQL function solution (but not stored procedure, etc) or PHP solution is acceptable. Thank you
Solution for first and last day of given quarter.
$q=ceil($date->format("n")/3);
$months_start=array('January','April','July','October');
$months_end=array('March','June','September','December');
$m_start=$months_start[$q-1];
$m_end=$months_end[$q-1];
$modifier='first day of '.$m_start.' '.$date->format('Y');
$date->modify($modifier);
echo $modifier.': '.$date->format('Y-m-d').'<br>';
$modifier='last day of '.$m_end.' '.$date->format('Y');
$date->modify($modifier);
echo $modifier.': '.$date->format('Y-m-d').'<br>';
You can use strtotime:
$date = strtotime('2013-01-16');
// First/last day of week
$first = strtotime('last Sunday');
$last = strtotime('next Saturday');
Or PHP's native DateTime functionality:
$date = new DateTime('2013-01-16');
// First/last day of month
$first = $date->modify('first day of this month');
$last = $date->modify('last day of this month');
Getting the first/last day of a year might be a little bit more tricky:
// Get date
$date = new DateTime('2013-01-16');
// Format to get date
$year = $date->format('Y');
// Get first day of Jan and last day of Dec
$first = $date->modify("first day of January $year");
$last = $date->modify("last day of December $year");
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