I'm trying to jump from month to month, starting from a specific timestamp, but when I get jump from August, September always gets skipped. Starting From August 31 (1346389200) and jumping 1 month:
strtotime('+1 month', 1346389200);
yields 1349067600 - which is October 1st.
I've read all about strtotime making mistakes if it doesn't have a starting date to calculate from, but what could the issue be with this?
Thanks
One month after 31 August is 31 September, but, because it not exists, php force the result to 1 Oktober.
So, you should force the current month on 1th day (of course if you want only year and month) :
strtotime('+1 month',strtotime(date("Y-m-1",1346389200)));
but if you use php >5.3 you can use more reliable DateTime class and methods.
You probably shouldn't use timestamps and strtotime for this comparison. You will introduce problems because of things like daylight savings, leap years, etc. Best to use DateTime and DateInterval classes/functions to do this in a more thorough manner.
http://php.net/manual/en/datetime.add.php
Related
For some reason, strtotime is not working correctly.
date('m', strtotime('Nov'));
is yielding 12.
What am I doing wrong?
If you don't provide a day, PHP will assume today which is the 31st. There is no 31st of November so PHP uses what would be the equivalent of the next day after the 30th which is the 1st of December. So you get 12 for December.
If you want an accurate date, be more specific. In this case, use the first day of the month.
date('m', strtotime('first day of Nov'));
Demo
I have the following example of me subtracting the DateInterval from DateTimeImmutable
$dateA = new DateTimeImmutable('2016-06-30');
$dateB = new DateTimeImmutable('2016-05-31');
$dateInterval = new DateInterval('P3M');
// print 2016-03-30 as expected
echo $dateA->sub($dateInterval)->format('Y-m-d');
// print 2016-03-02 which i would expect 2016-02-29
echo $dateB->sub($dateInterval)->format('Y-m-d');
When I set the period to 'P8M' it works as expected. How it comes, it dosent works for february?
Ok, it's really simple (kind of). Each 'month' interval evaluates to the prior (or X number of prior) month's equivalent day. If there are more days in the current month, than the month being landed on, the excess overflows to the following month.
So if you have a date which is May 31, 2016 and want to subtract 3 month intervals, it will:
Go back 3 months (in the list of months, don't think days yet), resulting in 'February'
Then look for February 31st. This doesn't exist so bleed over to following month 2 days (2016 Febuary has 29 days, so 2 extra days)
Viola! March 2nd.
Go forward, lets say you're in May 31, 2016 and want to add one month
Go forward one month to June.
Look for June 31st, nope, 1 extra day, bleed over to July.
As expected, July 1st is the answer.
The lesson in this: Adding and Subtracting Month intervals sucks, is confusing, and can lead to non-intuitive results unless you've got your month calculation rosetta stone with you.
Explanation from the PHP Docs
Note:
Relative month values are calculated based on the length of months that they pass through. An example would be "+2 month 2011-11-30", which would produce "2012-01-30". This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days.
If you have the day of the year. How can you convert that to day of month and month? For example: The day "144" should be converted to 26th of May. I guess I also have to add the actual year to account for leap years. But I haven't found anything at all.
For example the function mktime() exepects the month, year and day of month.
Anybody some suggestions?
The most reliable and convenient way is to use the DateTime object. You can use DateTime::createFromFormat() static method to create it based on day in the current year:
$date = DateTime::createFromFormat('z', '144');
And because you know have DateTime object in the $date variable, you can perform literally any task you want to. To output the contained date, simply call:
echo $date->format('j. n. Y');
It will print out 24. 5. 2012, because it's leap year and because it indexes days starting from zero (just like array indices).
strtotime("January 1st +".($days-1)." days");
This will return a timestamp corresponding to the specified day of the year.
I have a string like 09-10 which is representative of mm-dd. I need it in a format something like Monday 10th September? The problem is that I do not have a year and I can't have an array containing months and days because I would like to know the day of the week (Mon, Tue, Wed etc.)
Any idea how to do this in PHP, preferably using date() to format the date?
Note: this is not in MySQL...
You can't get the day (Monday, Tuesday etc) without knowing the year.
You can use date('jS F', strtotime('2012-09-10')); to get the day of the month and month, just shove any old year in there. I'd make sure to use a leap year year though to make sure you catch those pesky feb dates properly.
Example: http://codepad.org/JfUeTQlH
So, like this:
$d_m = '09-10';
$my_date = date('jS F', strtotime('2012-'.$d_m));
As it is clear Every 1st day of year is not Sunday, it happens after regular interval So just saying a date without year is not clear about day(Monday,Tuesday...). it will give you number of day in that year i.e. out of 365days. so if you are working within a year its OK, but if it goes beyond it You wont be able to get the day(Monday,Tuesday...).
Is there a simple way of, given a month and a year, establishing:
How many days there are in that month (factoring in leap years) Done
What day of the week the first day fall upon?
See http://php.net/manual/en/function.cal-days-in-month.php
$num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
and weekdays:
$weekday = date("l", mktime(0,0,0,$month,$day,$year));
$print ($weekday);
The latter is not very efficient but seems nicer than using getdate:
$my_t=getdate(date("U"));
print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");
Output like
Wednesday, September 29, 2011
You may find the answer to your questions with all needed variables and calculations by going to wikipedia. http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week
Take a look at the date function, particularly date('t') for the number of days in the month (ie the month given in time()) and date('t',$epoch) for the number of days of the month represented by the timestamp $epoch (which, of course, is given in epoch).
And for the day of the week, there's date('l',$epoch) (where the first argument is a lower-case 'L').