Getting the day before at end of month - php

Right now if you where to use strtotime('1 month') on January 31st you would get march 1st but I wondering if there is a simple way that if I where to run that same script on the same day instead of getting march 1st I could get February 28. Is this possible? Any suggestions would be greatly appreciated.
Thanks
EDIT:
Sorry I think I did not explain this properly, I want to be able to always get 30 days except for months where that day dont exists. So for instance if it is January 31st I want to be able to get February 28th but if it was January 10 I want February 10.

You could try a more verbose text:
$date = new DateTime('last day of next month');
echo $date->format('Y-m-d');
// => 2014-10-31
EDIT:
To answer your updated question, you can start with next month and compare it to last day of next month. If next month is larger than last day of next month, use last day of next month instead:
$date = new DateTime('next month');
if ($date > ($end = new DateTime('last day of next month'))) {
$date = $end;
}

Related

how to get previous month date range in php

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

PHP - Fetch Previous Month from Current Month

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

PHP strotime check + 10days to the first day of the next month

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.

Display the 1st, 2nd, 3rd, 4th, & 5th date of the current week in php

Ok, So im trying to figure out the best way in php to write (or print) the 1st, 2nd, 3rd, 4th, & 5th date of the current week (starting with Monday) in php.
For example... Using the week of the 4th through the 8th of February...
I would need a script for the 1st day of the week (starting with Monday) displayed as February 04, 2013 and then I would need the same script four more times to display Tues, Wed, Thurs, & Fri...
All-together I would end up with 5 scripts or one script that I could copy and manipulate to work the way I need it to...
Also, If you could tell me how to do the same for Saturday and Sunday that would be greatly appreciated as-well.
If there is anything that you do not understand, please let me know and I will try my hardest to clarify...
Thanks in advance!
First you need to get the "current" week's Monday. To do this, I would suggest calling date("N") and see if it's 1. If it is, then now is the Monday you want. Otherwise last monday is. Pass that to strtotime to get the timestamp corresponding to the first monday of the week. Then repeatedly add 24 hours (24*3600 seconds) to get each day.
$startofweek = date("N") == 1 ? time() : strtotime("last monday");
for($i=0; $i<5; $i++) {
$day = $startofweek + 24*3600*$i;
echo "Day ".($i+1).": ".date("d/M/Y",$day)."<br />";
}
You can use strtotime with special parameters. like:
$time = strtotime('monday this week');
$time = strtotime('today');
$time = strtotime('next monday');
$time = strtotime('previous monday');
Same goes for every other days of the week

DateTime modify string for first day of current year

im looking for the DateTime modify String for the first day of the year (now 1. January 2011). I tried the following:
<?php
$time = new DateTime();
// works as expected, the first day of the current month
$time->modify('first day of this month');
echo $time->format('c')."\n";
// this doesn't work. I also tried several other ways
$time->modify('first day of january');
echo $time->format('c')."\n";
>
I know there are other ways to retrieve the date, but I search an string for DateTime->modify() no other solution.
You should specify the year too, as you can see in this example:
"first day of January 2008"
from the official doc.
Update: It works on php version >= 5.3.6
On v5.5.6
echo date('Y-m-d', strtotime('first day of January this year'));
Result: 2013-01-01
To get the day of the week for the first of the year
or the first day of the month
<?php
//This is for a given month
$m="May";
// this id for this month
//$m=date('F');
//if you want the day of Sunday instead D use lower case l
echo date('D', strtotime('first day of January this year'));
echo "<br>". date("D", strtotime('first day of'. $m ));
?>
Result Wed For May with D
Result Wednesday with l
This work for me (PHP 5.6 - not tested on older version)... as we talk for DateTime object
//Get current datetime
$now = new DateTime();
$now->modify('first day of January this year');
echo $now->format('Y-m-d');
// Print (current year)-01-01
echo (new DateTime())->modify('first day of January this year')->format('Y-m-d');

Categories