strtotime() Get 7th day of -1 month - php

I have tried :
$firstOfMonth = "2015-01-01";
$last_month = date("Y-m-d", strtotime('first day of -1 month', strtotime($firstOfMonth)));
/* ^^^^^ This gives me 01 of last month */
/*** Tried 'seventh day of -1 month'
/* it gives 1970-01-01
*/
What I want is to get the 07 of last month.

uhm....the first day of a month is allways 01....so basically you only need year and month:
$firstOfMonth = "2015-01-01";
$last_month = date("Y-m-01",strtotime("-1 month",strtotime($firstOfMonth)));

Just like Mr.Manhattan said, you just need the year and month and to the seventh day you can leave it hard coded or in a variable:
date("Y-m-07",strtotime("-1 month"));
$fixedDay = '07';
echo date("Y-m-$fixedDay",strtotime("-1 month"));

Related

How to get the next month in PHP

I need to get the next month with php. Everywhere is written these example
date('Y-m-t', strtotime('+1 month'));
The output of the above code is '2017-03-31'. I need to get February, not March.
If you want to get the next irrespective of the current date in the current month. below code may help you
echo date('M',strtotime('first day of +1 month'));
// e.g. "Jan"
echo date('m',strtotime('first day of +1 month'));
// e.g. "1"
echo date('F',strtotime('first day of +1 month'));
// e.g. "January"
This will give you Next month.
You can find more formatting masks in date() function documentation
Use Like This
// One month from today
$date = date('Y-m-d', strtotime('+1 month'));
// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
To get next month using php use string to time function as below:
$nxtm = strtotime("next month");
echo date("M", $nxtm);
date("m", strtotime("2021-08-16 +1 Month"));
You can use the code below to get the next months first day.
$date = (new \DateTime('first day of next month'))->format('Y-m-d');
This is safer than 'next month' or '+1 month' because you may skip some months then.
If you want to have the next month with the same day as this month, or the last day of the next month if it otherwise would switch month you can use this function
function nextMonth()
{
$nextMonthNumber = date('M', strtotime('first day of +1 month'));
$nextMonthDate = new DateTime();
$nextMonthDate->add(new DateInterval('P1M'));
while ($nextMonthDate->format('M') != $nextMonthNumber) {
$nextMonthDate->sub(new DateInterval('P1D'));
}
return $nextMonthDate;
}

Date plus one month php [duplicate]

This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 6 years ago.
I have a small problem with dates in PHP.
When I made 31 + 1 month of January
with this code
$newDate = date('Y-m-d', strtotime('31-01-2016'.' + 1 month'));
echo $newDate;
it gives me 2 March but I need given me 29 February,
I need to add 1 month and is not 30days.
ditto for all dates:
for example
01 january + 1 month => 1 february
29 january + 1 month => 29 february
30 january + 1 month => 29 february
31 january + 1 month => 29 february
Thank for your help
I think you are looking for this type of dates.
<?php
$date = date('2016-01-31');
$currentMonth = date("m",strtotime($date));
$nextMonth = date("m",strtotime($date."+1 month"));
if($currentMonth==$nextMonth-1 && (date("j",strtotime($date)) != date("t",strtotime($date)))){
$nextDate = date('Y-m-d',strtotime($date." +1 month"));
}else{
$nextDate = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
}
echo "Next date would be : $nextDate";
?>
Check live demo : https://eval.in/610034
If date is 31-01-2016 then next date would be 29-02-2016
If date is 25-01-2016 then next date would be 25-02-2016
Simply try:
$date = new DateTime('2016-01-31');
$date->modify('last day of next month');
This of course only counts if you always go from the end of one moth to the end of the next one.
try this,
$date = "2016-01-29";
$date = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
echo $date;
https://3v4l.org/Y9PpV
How about something like this:
date_default_timezone_set('UTC');
$current_month = (int) date('m');
$year = date('y');
$newDate = date('Y-m-d', strtotime('31-1-2016'.' + 1 month'));
if($current_month == 12)
{
$new_month=0;
$year++;
}
$d = new DateTime( $year.'-'.($current_month+1).'-01' );
echo $d->format( 'Y-m-t' )."\n";
Change $current_month / $year based on your needs......

Calculate day of month with month, year, day of week and number of week

How can I calculate the day of month in PHP with giving month, year, day of week and number of week.
Like, if I have September 2013 and day of week is Friday and number of week is 2, I should get 6. (9/6/2013 is Friday on the 2nd week.)
One way to achieve this is using relative formats for strtotime().
Unfortunately, it's not as straightforward as:
strtotime('Friday of second week of September 2013');
In order for the weeks to work as you mentioned, you need to call strtotime() again with a relative timestamp.
$first_of_month_timestamp = strtotime('first day of September 2013');
$second_week_friday = strtotime('+1 week, Friday', $first_of_month_timestamp);
echo date('Y-m-d', $second_week_friday); // 2013-09-13
Note: Since the first day of the month starts on week one, I've decremented the week accordingly.
I was going to suggest to just use strtotime() in this fashion:
$ts = strtotime('2nd friday of september 2013');
echo date('Y-m-d', $ts), PHP_EOL;
// outputs: 2013-09-13
It seems that this is not how you want the calendar to behave? But it is following a (proper) standard :)
This way its a little longer and obvious but it works.
/* INPUT */
$month = "September";
$year = "2013";
$dayWeek= "Friday";
$week = 2;
$start = strtotime("{$year}/{$month}/1"); //get first day of that month
$result = false;
while(true) { //loop all days of month to find expected day
if(date("w", $start) == $week && date("l", $start) == $dayWeek) {
$result = date("d", $start);
break;
}
$start += 60 * 60 * 24;
}
var_dump($result); // string(2) "06"

What is best solution to get next and previous month from given date php

I want to get next and previous month from given date. this is my code.
$month = '2011-01-20';
$prevMOnth = funP($month);
$nextMonth = funN($month);
what is best solution to do that.
$next_month_ts = strtotime('2011-01-20 +1 month');
$prev_month_ts = strtotime('2011-01-20 -1 month');
$next_month = date('Y-m-d', $next_month_ts);
$prev_month = date('Y-m-d', $prev_month_ts);
Code mentioned before might not work in the end of months with 31 day (or March):
$prev_month_ts = strtotime('2011-01-20 -1 month');
This is a best solution to get name of previous month. Get this month first day's date, then subtract 1 day, then get month name:
date('F', strtotime('-1 day', strtotime(date('Y-m-01'))));
And get name of next month. Get this month last day's date, then add 1 day, then get month name:
date('F', strtotime('+1 day', strtotime(date('Y-m-t'))));
don't know if it's the best way to do it, but it's built into php, check out strtotime
EDIT:
sample code
$month = '2011-01-20';
$timestamp = strtotime ("+1 month",strtotime ($month));
$nextMonth = date("Y-m-d",$timestamp);
$date = "2012-01-25";
$priormonth = date ('m', strtotime ( '-1 month' , strtotime ( $date )));
$futuremonth = date ('m', strtotime ( '+1 month' , strtotime ( $date )));
echo $priormonth; // this will equal 12
echo "<br/>";
echo $futuremonth; // this will equal 02
The '-1 month' solution is unreliable when the month has 31 days (like ALeX inSide mentioned).
Here is a function that returns the date of any desired number of months before a given date: (it returns actually the 1st day's date)
function getAnyPreviousMonthDate( $monthsBefore = null, $startDate = null )
{
$monthsBefore = $monthsBefore ?? 1; //php7
$monthsBefore = abs($monthsBefore);
$c = $startDate ?? date('Y-m-d');
for($i==0; $i<$monthsBefore; $i++) {
$c = date('Y-m-d', strtotime('first day of previous month '.$c));
}
return $c;
}
so if we calle this like:
echo getAnyPreviousMonthDate(3);
// we will get the first day of past 3 months from now
echo getAnyPreviousMonthDate(1, '2015-10-31');
// will return: '2015-09-01'

php: strtotime("12/31/2004 +6 month")); not returning the last day of June

I expected this functional to return 6/30/2005 instead of 7/1/2005.
print date("m/d/Y", strtotime("12/31/2004 +6 month"));
Similarly, print date("m/d/Y", strtotime("1/31/2011 +1 month")) returns 03/03/2011 while would like it to return 2/28/2011.
Does anyone know if there is a straight forward way to show the last day of the added month?
How about this?
echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011
Demo
Edit: For your reference, here is a link to the documentation for relative times.
as strtotime continue in to next month if there isn't enoghe days that month,
you can back 6 month and check if its end up on the start date
$date2 = date("Y-m-d", strtotime("{$date} +6 months"));
$date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
if($date3 != $date)
{
$date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
}
(or in your case "m/t/Y")
One simple way is to actually go one month ahead of the day you want and then make the day value zero. Also, mktime() might be easier
$mymonth = 2; // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));
This should return 2/28/2011.
strtotime does the best it can with conflicting information. Saying
1/31/2011 +1month
would mean advancing to
2/31/2011
but February only has 28 (sometimes 29) days. 2011 isn't a leap year, so the "31st of February" gets normalized to "March 3rd".
The same applies for '12/31/2004 +6month'. That takes you to June 31st, 2005. But June only has 30 days, so the date is normalized to July 1st instead.

Categories