This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 9 years ago.
I have a simple variable that adds one month to today:
$endOfCycle = date("Y-m", strtotime("+1 month"));
Today is January 2013, so I would expect to get back 2013-02 but I'm getting 2013-03 instead. I can't figure out why it's jumping to March.
It's jumping to March because today is 29th Jan, and adding a month gives 29th Feb, which doesn't exist, so it's moving to the next valid date.
This will happen on the 31st of a lot of months as well, but is obviously more noticable in the case of January to Feburary because Feb is shorter.
If you're not interested in the day of month and just want it to give the next month, you should specify the input date as the first of the current month. This will always give you the correct answer if you add a month.
For the same reason, if you want to always get the last day of the next month, you should start by calculating the first of the month after the one you want, and subtracting a day.
This should be
$endOfCycle=date('Y-m-d', strtotime("+30 days"));
strtotime
expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
while
date
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
See the manual pages for:
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/function.date.php
You can use this code to get the next month:
$ts = mktime(0, 0, 0, date("n") + 1, 1);
echo date("Y-m-d H:i:s", $ts);
echo date("n", $ts);
Assuming today is 2013-01-31 01:23:45 the above will return:
2013-02-01 00:00:00
2
today is 29th of January, +1 month means 29th of Fabruary, but because February consists of 28 days this year, it overlaps to the next day which is March 1st
instead try
strtotime('next month')
Maybe because its 2013-01-29 so +1 month would be 2013-02-29 which doesn't exist so it would be 2013-03-01
You could try
date('m/d/y h:i a',(strtotime('next month',strtotime(date('m/01/y')))));
from the comments on http://php.net/manual/en/function.strtotime.php
$endOfCycle = date("Y-m", mktime(0, 0, 0, date("m", time())+1 , 15, date("m", time())));
try this:
$endOfCycle = date("Y-m", time()+2592000);
this adds 30 days, not exactly a month tough.
Related
Code:
$time = strtotime('2020-03-31');
echo date('Y-m-d', strtotime('-1 month', $time));
Expected Result: Any date from Feb 2020
Actual Result: 2020-03-02
Is there any better way to add or subtract a month from a given date?
Months are an awkward interval to work with, because they don't have a fixed length. Should the algorithm assume that by "1 month" you mean "30 days", or "31 days", or should it just try subtracting 1 from the "month" field in the date structure?
The last option is what is happening here: given "2020-03-31", PHP's date library is subtracting 1 from the "03" to give "2020-02-31". Since that's an invalid date (February 2020 had 29 days), it then "normalises" it to a real date - 2 days after the 29th February was the 2nd March.
Probably you want to use a more specific period to subtract, like 30 days - although note that if the initial input is "2020-03-01" that will give you "2020-01-31", not "2020-02-01".
Ultimately, this is a problem with our irregular calendar, rather than with PHP. It's really up to you to define what you mean by "a month before", and use a more specific algorithm that captures that requirement.
You can make code like below
<?php
$time = strtotime('2020-03-1 -32 days');
echo date('M-Y', $time); // output Feb-2020
?>
The above code will return date as you expected
I am using a date() format to return the starting weekday of a month. The code I have below is how I am attempting to achieve this. For the current year (2018) this works as normal. For example This month is august and the starting weekday is a Wednesday so it will return a 3 for Wednesday. (It works so far)
As we advance the year to 2019 it starts to get the starting weekday wrong.
For example January 2019 starts on a Tuesday so it should return 2 but returns 1. (one day out)
This error seems to be cumulative so if we go to 2020 then it is 2 days out etc.
I have tried so hard to format this Date() correctly but to no avail. Is this even the correct way to do this?
Code:
$future_month = 5 /*for January 2019*/
$starting_weekday = date('N',mktime(0, 0, 0, date('m', strtotime('+'.$future_month.' months', strtotime(date('Y-m-01')))), 1));
Many Thanks
Cameron
Your code makes this much more complicated than it needs to be.
$dt = new DateTime('first day of +5 months')
$dt->format('N'); // "2"
i am trying to get the day of week from a timestamp:
an example of the timestamp could be:
2014-09-14 18:28:11
I have tried with the following code:
$date = date("D", strtotime($activity[$i]['timestamp']));
However the result i get here is:
Thu
which should have been sunday?
Also is it possible to get it as a full discription instead of a short version of the day name?
Answer to part two of the question is that you can just use l (lowercase 'L') and it'll output Sunday instead of Sun.
$date = date("l", strtotime($activity[$i]['timestamp']));
As for the first part, it probably output Thursday, 1 January 1970 because it received an error instead of an actual date as argument to strtotime.
How would you select all records between the 8th of this month and the 8th of last month without passing in the current month? My date column is date type.
SELECT *
FROM table
WHERE date BETWEEN '8th of last month' AND
'8th of this month'
I've tried working out the date calculations in PHP then sending them to MySQL. But it gets a bit complicated when factoring in years. I'm wondering if this could be accomplished with SQL instead?
Using PHP might be more straightforward from a readability standpoint using relative time with strtotime().
The following should get you started:
echo date('Y-m-d', strtotime('eighth of last month'));
UPDATE
Ordinals don't work as I thought above. However, assuming you use the same day of the month (i.e 8th). The following is fine:
echo date('Y-m-08', strtotime('last month'));
Here is a solution
echo date('Y-m-d', mktime(0, 0, 0, date('n') - 1, 8));
If you want to use strtotime
echo date('Y-m-d', strtotime('+7 days', strtotime('first day of last month')));
Being that the current month/year is January 2012, why does the following code return December 2011 and not November 2011?
echo date("F Y", strtotime("-2 months"));
This is on PHP 5.3.0 if it makes a difference.
To get what you are looking for you can use this rather verbose version instead:
echo date("F Y", strtotime("first day of this month - 2 months"));
The problem with your original version is described in detail here: http://derickrethans.nl/obtaining-the-next-month-in-php.html. Quoted below:
Over and over again PHP users complain that next month in PHP's
date-string parser doesn't go to the next month, but instead skips to
the one after next month; like in the following example:
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'next month' );
echo $d->format( 'F' ), "\n";
?>
The output of the little script will be March. March obviously doesn't
follow January as February is in between. However, the current
behavior is correct. The following happens internally:
next month increases the month number (originally 1) by one. This
makes the date 2010-02-31. The second month (February) only has 28
days in 2010, so PHP auto-corrects this by just continuing to count
days from February 1st. You then end up at March 3rd. The formatting
strips off the year and day, resulting in the output March. This can
easily be seen when echoing the date with a full date format, which
will output March 3rd, 2010:
This is for adding months, but the same applies in reverse when subtracting months; there was no November 31st, so the strtotime method "corrects" it into December 1st.