strtotime miscalculates date when adding days that cross year - php

I need to add days to a date. I have been happily using the following and it's worked up until the end of the year:
date('o-m-d', strtotime("{$start_date} +6 days"));
As an example, when $start_date equals 2022-12-19, the result is 2022-12-25:
date('o-m-d', strtotime("2022-12-19 +6 days")); // 2022-12-25
This falls to pieces when the number of days lands on the first day of the year - so for 2022-12-26 the result is 2022-01-01 (2022, rather than 2023!)
date('o-m-d', strtotime("2022-12-26 +6 days")); // 2022-01-01, which is incorrect!
Playing around I found it's only if the date lands on the first day of the year - here are some examples:
date('o-m-d', strtotime("2022-12-30 +1 days")); // 2022-12-31, correct
date('o-m-d', strtotime("2022-12-30 +2 days")); // 2022-01-01, incorrect!
date('o-m-d', strtotime("2022-12-30 +3 days")); // 2023-01-02, correct
date('o-m-d', strtotime("2023-01-03 -1 days")); // 2023-01-02, correct
date('o-m-d', strtotime("2023-01-03 -2 days")); // 2022-01-01, incorrect!
date('o-m-d', strtotime("2023-01-03 -3 days")); // 2022-12-31, correct
I've seen when I include the h:m:s, I get a weird time of day, but that would surely impact all the date calculations. Anyone have an idea what's going on here? (I'm using php#7.4)
I've tried all sorts of variations, like adding the base date as the second parameter to strtotime, even trying relative to today (-11 days at time of writing) and always the same error..
To be clear, adding two days to 2022-12-30 should result in 2023-01-01, not 2022-01-01.
I've looked through other answers to similar questions, such as using
$date = new \DateTime('2022-12-26');
$end_date = date_add($date, date_interval_create_from_date_string("6 days"))->format('o-m-d');
which results in exactly the same error.

Use Y for the year, not o. o is:
ISO 8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
http://www.php.net/manual/en/datetime.format.php

Related

Is there a way to get the x-th day of the next month in PHP?

I need to get the 4th day of the next month in PHP. Should be able to generate the date using strtotime()
I followed https://www.php.net/manual/en/datetime.formats.relative.php
tried strtotime('first day of next month +4 days') but it returns first day of next month always because of the precendence I think.
Here is a code you can try. It is my hope that this will work for you
<?php
$d1 = date("Y-m-d", strtotime('first day of next month'));
echo date('Y-m-d', strtotime('+3 days', strtotime($d1)));
Thanks.

PHP dateTime previous month of 31 Dec is wrong

I have this:
$previousMonth = new DateTime('2019-12-31');
$previousMonth->modify('-1 month');
My understanding is '-1 month' should modify the object regardless of number of days in that month, or?
Naturally what should I get or expect to get is end of Nov(2019-11-30) but what I get is first of December(the same month).
BTW if I change the date to '2019-12-30'(one day prior) then it will be end of Nov.
If my initial assumption is not correct, then what is the best alternative to reliably calculate the previous month?
Any thoughts?
The simplest and easiest way to get the last month in php is
$previousMonth = date("Y-n-j", strtotime("last day of previous month"));
Same as been suggested on other thread Getting last month's date in php
$date = "2019-12-31 00:00:00";
echo date('Y-m-d', strtotime($date . '-1month'));
This prints out 2019-12-01 as the 31/11 does not exist.
The following doesn't answer your question but may help in the future. I like to use Carbon when working with dates. Your issue could be resolved quite simply with this.
https://carbon.nesbot.com/
It has many functions and is extremely simple to use, and it can be installed with Composer.
To get the last day of the previous month, you can get the first day of the current month and substract 1 second or 1 day :
$previousMonth = new DateTime('2019-12-31');
$previousMonth->modify($previousMonth->format('Y-m-01')); // date is 2019-12-01 00:00:00
$previousMonth->modify('-1 sec');
echo $previousMonth->format('Y-m-d H:i:s') . PHP_EOL; // Outputs 2019-11-30 23:59:59
$previousMonth->modify('+1 sec'); // set back the original date 2019-12-01 00:00:00
$previousMonth->modify('-1 day');
echo $previousMonth->format('Y-m-d H:i:s'); // Outputs 2019-11-30 00:00:00

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.

Set up end of this week, next of this month and end of next month dates

I'm trying to set up a few variables to then pass into my code to add some classes. I know how to do most of it, but it's the setting of the variables that's difficult.
I need to set up three variables that will be compared with the current date.
<?php $current_date = date('Ymd'); ?>
The three variables are: this week, this month and next month.
I had originally set my this week variable as so:
<?php $current_date_week = date('Ymd', strtotime($current_date . ' +7 days')); ?>
But I'm getting this wrong. It shouldn't be from today's date + 7, it should get the date of the end of the current week using the Ymd date format. So if today's date is 20140326 then my $current_date_week should bring back 20140330 and so on.
This month should do the same, get the last day of the current month in Ymd format and next month should get the last day of the next month.
I hope this made sense.
$current_date_week
$current_date_month
$current_date_month_next
Thanks,
R
Do this way:
$current_date_week = date('Ymd', strtotime('this Sunday')); //20140330
$current_date_month = date('Ymd', strtotime('last day of this month'));//20140331
$next_date_month = date('Ymd', strtotime('last day of next month'));//20140430
check available list for formats.
Use these as second arguments to date():
mktime(1,2,3,date('m'),date('d')+7-date('N'),date('Y'));
mktime(1,2,3,date('m')+1,0,date('Y'));
mktime(1,2,3,date('m')+2,0,date('Y'));

How to get previous month and year relative to today, using strtotime and date?

I need to get previous month and year, relative to current date.
However, see following example.
// Today is 2011-03-30
echo date('Y-m-d', strtotime('last month'));
// Output:
2011-03-02
This behavior is understandable (to a certain point), due to different number of days in february and march, and code in example above is what I need, but works only 100% correctly for between 1st and 28th of each month.
So, how to get last month AND year (think of date("Y-m")) in the most elegant manner as possible, which works for every day of the year? Optimal solution will be based on strtotime argument parsing.
Update. To clarify requirements a bit.
I have a piece of code that gets some statistics of last couple of months, but I first show stats from last month, and then load other months when needed. That's intended purpose. So, during THIS month, I want to find out which month-year should I pull in order to load PREVIOUS month stats.
I also have a code that is timezone-aware (not really important right now), and that accepts strtotime-compatible string as input (to initialize internal date), and then allows date/time to be adjusted, also using strtotime-compatible strings.
I know it can be done with few conditionals and basic math, but that's really messy, compared to this, for example (if it worked correctly, of course):
echo tz::date('last month')->format('Y-d')
So, I ONLY need previous month and year, in a strtotime-compatible fashion.
Answer (thanks, #dnagirl):
// Today is 2011-03-30
echo date('Y-m-d', strtotime('first day of last month')); // Output: 2011-02-01
Have a look at the DateTime class. It should do the calculations correctly and the date formats are compatible with strttotime. Something like:
$datestring='2011-03-30 first day of last month';
$dt=date_create($datestring);
echo $dt->format('Y-m'); //2011-02
if the day itself doesn't matter do this:
echo date('Y-m-d', strtotime(date('Y-m')." -1 month"));
I found an answer as I had the same issue today which is a 31st. It's not a bug in php as some would suggest, but is the expected functionality (in some since). According to this post what strtotime actually does is set the month back by one and does not modify the number of days. So in the event of today, May 31st, it's looking for April-31st which is an invalid date. So it then takes April 30 an then adds 1 day past it and yields May 1st.
In your example 2011-03-30, it would go back one month to February 30th, which is invalid since February only has 28 days. It then takes difference of those days (30-28 = 2) and then moves two days past February 28th which is March 2nd.
As others have pointed out, the best way to get "last month" is to add in either "first day of" or "last day of" using either strtotime or the DateTime object:
// Today being 2012-05-31
//All the following return 2012-04-30
echo date('Y-m-d', strtotime("last day of -1 month"));
echo date('Y-m-d', strtotime("last day of last month"));
echo date_create("last day of -1 month")->format('Y-m-d');
// All the following return 2012-04-01
echo date('Y-m-d', strtotime("first day of -1 month"));
echo date('Y-m-d', strtotime("first day of last month"));
echo date_create("first day of -1 month")->format('Y-m-d');
So using these it's possible to create a date range if your making a query etc.
If you want the previous year and month relative to a specific date and have DateTime available then you can do this:
$d = new \DateTimeImmutable('2013-01-01', new \DateTimeZone('UTC'));
$firstDay = $d->modify('first day of previous month');
$year = $firstDay->format('Y'); //2012
$month = $firstDay->format('m'); //12
date('Y-m', strtotime('first day of last month'));
strtotime have second timestamp parameter that make the first parameter relative to second parameter. So you can do this:
date('Y-m', strtotime('-1 month', time()))
if i understand the question correctly you just want last month and the year it is in:
<?php
$month = date('m');
$year = date('Y');
$last_month = $month-1%12;
echo ($last_month==0?($year-1):$year)."-".($last_month==0?'12':$last_month);
?>
Here is the example: http://codepad.org/c99nVKG8
ehh, its not a bug as one person mentioned. that is the expected behavior as the number of days in a month is often different. The easiest way to get the previous month using strtotime would probably be to use -1 month from the first of this month.
$date_string = date('Y-m', strtotime('-1 month', strtotime(date('Y-m-01'))));
I think you've found a bug in the strtotime function. Whenever I have to work around this, I always find myself doing math on the month/year values. Try something like this:
$LastMonth = (date('n') - 1) % 12;
$Year = date('Y') - !$LastMonth;
date("m-Y", strtotime("-1 months"));
would solve this
Perhaps slightly more long winded than you want, but i've used more code than maybe nescessary in order for it to be more readable.
That said, it comes out with the same result as you are getting - what is it you want/expect it to come out with?
//Today is whenever I want it to be.
$today = mktime(0,0,0,3,31,2011);
$hour = date("H",$today);
$minute = date("i",$today);
$second = date("s",$today);
$month = date("m",$today);
$day = date("d",$today);
$year = date("Y",$today);
echo "Today: ".date('Y-m-d', $today)."<br/>";
echo "Recalulated: ".date("Y-m-d",mktime($hour,$minute,$second,$month-1,$day,$year));
If you just want the month and year, then just set the day to be '01' rather than taking 'todays' day:
$day = 1;
That should give you what you need. You can just set the hour, minute and second to zero as well as you aren't interested in using those.
date("Y-m",mktime(0,0,0,$month-1,1,$year);
Cuts it down quite a bit ;-)
This is because the previous month has less days than the current month. I've fixed this by first checking if the previous month has less days that the current and changing the calculation based on it.
If it has less days get the last day of -1 month else get the current day -1 month:
if (date('d') > date('d', strtotime('last day of -1 month')))
{
$first_end = date('Y-m-d', strtotime('last day of -1 month'));
}
else
{
$first_end = date('Y-m-d', strtotime('-1 month'));
}
If a DateTime solution is acceptable this snippet returns the year of last month and month of last month avoiding the possible trap when you run this in January.
function fn_LastMonthYearNumber()
{
$now = new DateTime();
$lastMonth = $now->sub(new DateInterval('P1M'));
$lm= $lastMonth->format('m');
$ly= $lastMonth->format('Y');
return array($lm,$ly);
}
//return timestamp, use to format month, year as per requirement
function getMonthYear($beforeMonth = '') {
if($beforeMonth !="" && $beforeMonth >= 1) {
$date = date('Y')."-".date('m')."-15";
$timestamp_before = strtotime( $date . ' -'.$beforeMonth.' month' );
return $timestamp_before;
} else {
$time= time();
return $time;
}
}
//call function
$month_year = date("Y-m",getMonthYear(1));// last month before current month
$month_year = date("Y-m",getMonthYear(2)); // second last month before current month
function getOnemonthBefore($date){
$day = intval(date("t", strtotime("$date")));//get the last day of the month
$month_date = date("y-m-d",strtotime("$date -$day days"));//get the day 1 month before
return $month_date;
}
The resulting date is dependent to the number of days the input month is consist of. If input month is february (28 days), 28 days before february 5 is january 8. If input is may 17, 31 days before is april 16. Likewise, if input is may 31, resulting date will be april 30.
NOTE: the input takes complete date ('y-m-d') and outputs ('y-m-d') you can modify this code to suit your needs.

Categories