This question already has an answer here:
date_create_from_format() returns wrong value. [duplicate]
(1 answer)
Closed 6 years ago.
I am trying to create data from format using Datetime Class as following
$date = DateTime::createFromFormat('m-Y', '02-2016');
echo $date->format('Y-m-d');
Out put 2016-03-01, i was expecting to get 2016-02-01
is it a bug? or i am understating this function in a wrong way?
If you provide month and year, then PHP will provide a default day, using the current day of the current month (ie 30) today
That gives an effective 2016-02-30, which isn't a valid date..... but PHP allows these types of values where days is higher/lower than days in month, months are higher/lower than months in the year, and just increments/decrements to a valid date..... in this case, 1 additional day after the last day in month 2 or 2016 (29th February 2016) to give 1st March 2016
EDIT
Reference for day/month overflow/underflow behaviour
Note:
It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes "2008-08-00" equivalent to "2008-07-31" and "2008-06-31" equivalent to "2008-07-01" (June only has 30 days).
It is also possible to underflow the mm and MM formats with the value 0. A month value of 0 means December of the previous year. As example "2008-00-22" is equivalent to "2007-12-22".
If you combine the previous two facts and underflow both the day and the month, the following happens: "2008-00-00" first gets converted to "2007-12-00" which then gets converted to "2007-11-30". This also happens with the string "0000-00-00", which gets transformed into "-0001-11-30" (the year -1 in the ISO 8601 calendar, which is 2 BC in the proleptic Gregorian calendar).
It's an known bug... or a documented feature, depending on how you look at it. See Mark Baker's answer for more details.
Workaround :
To get the date you expect, just add 01 to your input as a value for the day of the month.
$input = '02-2016';
$date = DateTime::createFromFormat('d-m-Y', '01-' . $input);
echo $date->format('Y-m-d');
// OUTPUT : 2016-02-01
Related
I am try to get the year and week of year off of a given date in my code:
$dueDate->format('W , Y');
In the code above, duedate is a datetime object with this date value:
December 31, 2018
When I output the format I specified above, I get this:
01 , 2018
Looking at each value separately the function is correct. However, together it is confusing.
It seems to be reading December 31st as the first week because it falls on a Monday, so technically it is right, it is the first week of 2019. In that case though, I would want the year to roll over and read 2019.
How can I resolve this to roll over the year in this case only? Any help is appreciated.
You need to use the ISO-8601 week numbering year which is o if you want the year for the ISO-8601 week. From the docs:
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. (added in PHP 5.1.0)
$dueDate->format('W , o');
I am try to get the year and week of year off of a given date in my code:
$dueDate->format('W , Y');
In the code above, duedate is a datetime object with this date value:
December 31, 2018
When I output the format I specified above, I get this:
01 , 2018
Looking at each value separately the function is correct. However, together it is confusing.
It seems to be reading December 31st as the first week because it falls on a Monday, so technically it is right, it is the first week of 2019. In that case though, I would want the year to roll over and read 2019.
How can I resolve this to roll over the year in this case only? Any help is appreciated.
You need to use the ISO-8601 week numbering year which is o if you want the year for the ISO-8601 week. From the docs:
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. (added in PHP 5.1.0)
$dueDate->format('W , o');
I failed to find a proper solution to this issue. As you see in Example #3 in the PHP documentation, they state that one must beware when adding months using the DateInterval in DateTime::add.
There's not really any explanation for why the method's behavior is as such and what I can do to avoid this, which I find to be an error at first sight.
Anyone have some insight into this?
The issue is that each month can have a different number of days in them. The question is what you're doing when you want to increment a date by 1 month. Per the PHP documentation if you're on January 31st (or 30th) and you add 1 month, what is the expected behavior?
February only has 29 days in it. Do you want to be set to the last day of the month? You're generally safer incrementing by a set number of days if that's what you're looking for, or a static date based on the current date. Without knowing what you're trying to accomplish when you increment your month, it's tough to say how to watch for an error.
EDIT:
As someone mentions in the similar post commented by Mike B above, you probably want to do something where you (in pseudocode):
1) Use cal_days_in_month() for the next month and save that number to a variable x
2) If x >= current billing DOB, increment and be done
3) DateTime::modify('last day') (havent used this before but something along these lines) to set the date to the last date of the next month (set it to the 1st of the next month, then last day?)
Worth noting is that if you use the variable here as the new billing value, you'll wipe out your original value. I would save an extra DB value that's "first billing date" or just "billing_day_of_month" or something, and use that to figure out the day of month that you should be looking at
If your goal is to strictly increment by user-friendly months (thus, 3 months from January 21st should be April 21st), with the exception that shorter membership months get shortened (thus, 1 month from January 31st is February 28th/29th), then you only need to go back a few days if you crossed over into the next month:
function addMonths($date,$months) {
$orig_day = $date->format("d");
$date->modify("+".$months." months");
while ($date->format("d")<$orig_day && $date->format("d")<5)
$date->modify("-1 day");
}
$d = new DateTime("2000-01-31");
addMonths($d,1);
echo $d->format("Y-m-d"); // 2000-02-29
I found a possible bug in PHP 5.6. The function strftime with a parameter of %G generates the 4 digit year. However, it seems to return the wrong year when fed 1483246800 - i.e. Jan 1, 2017. It returns 2016 instead!
Example code snippet:
echo strftime('%G', strtotime('2017-01-01'));
This should print "2017", but I get "2016" instead. I am running PHP 5.6.
This edge case also shows up for other years - e.g. 2016-01-03 outputs 2015 instead of 2016.
It's not a bug.
As someone pointed out in answer to a bug report someone else filed a while back:
%G - The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead...A reminder about ISO week numbers. They begin on mondays, end on sundays, and are considered a part of the year in which the majority of their days fall.
This means that using %G for a date close to a year's beginning/end could give you the correct year, the previous year, as in your case, or the next year, (for example, echo strftime('%Y', strtotime('2002-12-30)) gives 2003).
If you want to get the correct year, you should use %Y instead. echo strftime('%Y', strtotime('2017-01-01')); gives 2017.
It's also worth checking out the definition of %V:
ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week.
This question already has answers here:
Using strtotime for dates before 1970
(7 answers)
Closed 7 years ago.
Hello guys is there a way to convert the date 0001-01-1 to a time format? I am trying to to use the php function strtotime("0001-01-01"), but it returns false.
My goal is to count the days from the date: 0001-01-01 to: 2011-01-01.
Use DateTime class. strtotime returns a timestamp which in your case will be out of int bounds.
As Ignacio Vazquez-Abrams has commented, dates before the adoption of the Gregorian calendar are going to be problematic:
The Gregorian calendar was adopted at different times in different countries (anywhere from 1582 to 1929). According to Wikipedia, a few locales still use the Julian calendar.
Days needed to be "removed" to switch to the Gregorian calendar, and the exact "missing" dates differ between countries (e.g. the missing days in September 1752). Some countries tried to do a gradual change (by removing February 29 for a few years); Sweden switched back to the Julian calendar to "avoid confusion", resulting in a year with February 30.
Years were not always counted from January 1 (which has left its legacy in things like the UK tax year).
Michael Portwood's post The Amazing Disappearing Days gives a reasonable summary.
In short, you have to know precisely what you mean by "1 Jan 1 AD" for your question to make any sense. Astronomers use the Julian Day Number (not entirely related to the Julian calendar) to avoid this problem.
EDIT: You even have to be careful when things claim to use the "proleptic Gregorian calendar". This is supposed to be the Gregorian calendar extended backwards, but on Symbian, years before 1600 observe the old leap year rule (i.e. Symbian's "year 1200" is not a leap year, where in the proleptic Gregorian calendar it is).
I am afraid it wont work since strtotime changes string to timestamp, which have lowest value of 0, and its at 1970-01-01.. cant go lower than that..
date('Y-m-d', 0);
You cannot do that this way. Here is one option how to do this.
$date1= "2011-01-01";
$date2 = "2011-10-03";
//calculate days between dates
$time_difference = strtotime($date2) - strtotime($date1);
now you got time difference in seconds from wich you can get days, hours, minutes, seconds.
From the manual:
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
You may want to give 'DateTime::createFromFormat' a shot instead this.
Also note that you cannot go below 1901 on a 32-bit version of PHP.
You can't use the function strtotime because this function return a timestamp and the time stamp start from 1970 so i advise you to searsh for a different way