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
Related
22nd Jan is the 3rd week of the year. My course started from 10th Jan, respect to that 22nd Jan should be the 2nd week. how can I find this using MySQL query and PHP?
I am using this WEEK(DATE_FORMAT(FROM_UNIXTIME(a.duedate), '%Y-%c-%d ')).
Week calculations mainly begin from the beginning of a month or year. You can try something like the below code; in this case, the date will be fixed.
echo Date("Y-m-d", strtotime("2013-01-31 +1 Month -3 Day"));
// 2013-02-28
Reference: http://php.net/manual/en/function.date.php
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
I want to get the year of the previous month.
For example in July 2012 I want result 2012.
But in January 2013 I want result 2012
and in Feb 2013 I want result 2013
I have searched and found this How to get previous month and year relative to today, using strtotime and date?
It uses date_create function (i.e. DateTime Class). But I want a solution without using it. i.e. by using simple date() function
<?php
echo date('Y', strtotime('-1 month'));
Should work as expected.
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...).
I'm trying to get the starting monday date of the actual week we are in. Like we are July 15th and would like to have the result with strtotime to return July 11th...
I've tried
strtotime("-1 weekdays");
Without luck... What could be the function for the actual week please.
Thanks
$timestamp = strotime('monday this week');
Will give you Monday! ;)
Did none of these help you to find your answer?
Get first day of week in PHP?
Finding First day of week via php
First date of a week