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
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');
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 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 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...).