I'm using php date('W'); function to get a number of the week of this year.
date('W') says "40" (that's correct).
The problem is, that my Linux machine, CentOS has a correct date, but shows incorrect week number.
date +"%W" says "39"
Does anyone know why it works this way and how could I fix it?
Okay, I found the answer:
I need to use it like this:
date +%V
%V ISO week number, with Monday as first day of week (01..53)
%W week number of year, with Monday as first day of week (00..53)
date('W');
The code above gives the ISO-8601 week number (don't know what Linux shows by default). If you want the correct year there as well, use
date('W o');
instead of (W Y), because the o gives the right year with the W-week.
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 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.
EDIT: Two minuses in under a minute. I thought this was a tiny but interesting query.
ORIGINAL QUERY: I am generating a five week calender into which I will pour info (the day and date and other stuff).
I want the top left cell to be the "current" Sunday.
For example if today is Weds 12th then I need to find Sun 9th as the start for the run. Then I just do a $var = strtotime("+1 day", $var) for the next 34 slots.
My problem is doing this neatly if today is Sunday.
At present I have:
date_default_timezone_set("Pacific/Honolulu");
$day_now = time();
$current_day= date ("D", $day_now);
if ($current_day == "Sun")
{$day_now = strtotime("+1 day", $day_now);}
$day_now = strtotime("last sunday");
//do stuff/
I just wondered if there was a more "tidy" way of doing this.
I tried "this sunday, last sunday, sunday this week" but could find nothing that would pick today as the Sunday and ALSO work for the rest of the week.
Just curious if anyone has found a form of words that works for this with strtotime.
OK strtotime has NO one phrase solution to the current query ('this sunday" for the whole week). (I would call it a bug!)
The linked discussions
Strange behaviour of strtotime() when using relative dates ('this week')
and
Computing relative dates in php using strtotime()
offer solutions.
I like my current solution - while not elegant it is very readable and I know that if I come back to it in a couple of years I will understand what is going on.
Hopefully "this sunday" will be fixed someday instrtotime.
Thanks for the input.
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...).