Get the curent week of a month [duplicate] - php

This question already has answers here:
PHP get number of week for month
(30 answers)
Closed 9 years ago.
I need a php code which return the current week of the day in a month.
Eg: today is 03 / 12/ 2013 , which is first week of this month
if the date becomes 10 / 12 / 2013, then the day is in second week of this month.
I need the php code which retur current week of the month, so the value will always be from 1 to 5.
Does anyone know this code to get the value.
Any help will be appreciated.
Thanks In Advance.
--
Tibin Mathew

I guess you just divide the day by 7?
$DoM = date("j");
switch(true) {
case $DoM <= 7:
//this is week 1
break;
case $DoM <= 14:
//this is week 2
break;
case $DoM <= 21:
//this is week 3
break;
case $DoM <= 28:
//this is week 4
break;
default:
//only possibility left is week 5
}
OR
Did you want to consider this based on a certain start day of the week? In that case you'd have a range 1-6 (e.g. 1st is a Saturday and start of week is Sunday would mean 31st would be in week 6...).

This crude little code with give you a rough estimate of what week of the month it is.
$date = date("W"); // Get the week of the year
$week = ($date % (52 / 12) ); // The division remainder (modulo) of the average number of weeks per month will tell you what week of the month you are.
It works well when I tested it for today, next week, 6th, 9th week of the year and so on. But it's not accurate because number of weeks per month never evenly average out (eg: Feb).
Hope that helps. And also I'm interested in improved answers.

Related

Get right week from date [duplicate]

This question already has answers here:
Get previous week number from php week number
(6 answers)
Closed 8 months ago.
Lets say i have date 2022-01-02 and when I want to get week of year I´ve got 52. But its the 52 week of year 2021, not 2022. So question is how can I get the right year of week from this date?
$date = "2022-01-02 00:00:00";
$week = (float) date("W", strtotime($date));
print $week . "\n";
Week 52 of year 2021 started at 2021-12-27 (a Monday) and ended at 2022-01-02 (a Sunday). Week 1 of year 2022 started at 2022-01-03 (a Monday). This is the definition by ISO 8601. Such a numbering of the weeks is used, for example, in international merchandise management.
For this, date() support 3 format characters:
w for week day
W for the week number
o for the year related to W
So echo date("o W w",strtotime("2022-01-02 00:00")); prints 2021 52 0 (year, week, day of week). The day-of-week follows the old idea, where Sunday is the first day of the week. The the week days are numbered 1 2 3 4 5 6 0.

php week(curdate()) sum of other years

OK so i have this query:
SELECT obrero as MAESTRO, sum(costo_semanal) AS TOTAL_COST,
ROUND(SUM(week_cost)/MONTH(CURDATE()),2) AS MONTHLY_COST,
ROUND(SUM(week_cost)/WEEK(CURDATE()),2) AS WEEKLY_COST
from tbl_costos WHERE obrero ='$maestro'
I did this and it worked great in 2015, the problem is that now on 2016 we go back to week 1 and month 1 so im not having the proper division.
What I need to accomplish is to sum the 52 weeks of the past year and sum the current week of this year so i could have a % of the cost per week
"cost/number of weeks" = $cost per week.
for example of today 2016-01-18 being the 4th week of the year
total paid (of 2015 and 4 weeks of 2016) = $4000.00
weeks = 52 + 4 = 56
4000.00/56 = $71.4285714 average cost per week
The same thing applies to Months, it should be doing the division with 13, and cus January is month 1, its doing it over 1.
I could just do:
SUM(week_cost)/(12+ MONTH(CURDATE()));
and
SUM(week_cost)/(52 + WEEK(CURDATE()));
but that would solve the problem for this year only!!
You should use DATEDIFF() in order to calculate the number of days each maestro has worked from a given date, "start_date" to a given end date "end_date", and then convert those days to weeks, months and years if you need to.
Be flexible and anticipate that each maestro can work from any given date, or a report can be asked from any given date. Your's assume that each maestro started to work on 2015-01-01 and that all reports shall be done with this in mind. Reality is different.
Select obrero as MAESTRO, sum(costo_semanal) as TOTAL_COST, ROUND(SUM(costo_semanal/ROUND(DATEDIFF(start_date, end_date)/30,0)),0) AS MONTHLY_COST, ROUND(SUM(costo_semanal/ROUND(DATEDIFF(start_date, end_date)/7,0)),0) AS WEEKLY_COST FROM tbl_costos WHERE obrero=$maestro;
Do not place the variable $maestro on your query, better use PREPARED STATEMENTS.

Irregular output in getting the week number of January

Can someone explain why this is so?
<?php
echo date("W", strtotime("2015-01-01"))."<br>"; //returns 1
echo date("W", strtotime("2014-01-01"))."<br>"; //returns 1
echo date("W", strtotime("2016-01-01"))."<br>"; //returns 53
?>
When outputing the week number of the day in the month , result is incorrect for some cases. Here the output, week number is 1 which is true for first two cases but not in the third case.
The reason it outputs 53 and not 1 for "2016-01-01" is that it counts the first week of the year from the first monday of the year which in the year 2016 lies on the 4th of Jan ("2016-01-04").
W ISO-8601 week number of year, weeks starting on Monday (added in PHP
4.1.0) Example: 42 (the 42nd week in the year)
See full documentation for php date function here.
As mentioned, W ISO-8601 week number of year is the number that you get.
For more information on W ISO-8601 week number of year see below,
An ISO week-numbering year (also called ISO year informally) has 52 or
53 full weeks. That is 364 or 371 days instead of the usual 365 or 366
days. The extra week is referred to here as a leap week, although ISO
8601 does not use this term. Weeks start with Monday. The first week
of a year is the week that contains the first Thursday of the year
(and, hence, always contains 4 January). ISO week year numbering
therefore slightly deviates from the Gregorian for some days close to
1 January.
Source: Wiki
2016-01-01 belongs to the last week of year 2015, as the week begins in that year, so the result is correct.

PHP - Calculate repeating date based on day of week?

I'm trying to figure out how to repeat certain tasks on specific days at certain intervals based on an existing date.
For example, I have this:
Date Task
2011-01-12 MJK-0083
I want to:
Determine the day of the week based on the date provided (in this case, Wednesday)
Determine which Wednesday of the month it is (in this case, the 2nd Wednesday)
Calculate the date of the 2nd Wednesday in June as the next date this task will occur
I've been looking at some examples, but while I can do bits of it, I can't seem to figure out how to do all of it. For example, I'm using this for step 1:
date('l', strtotime('2011-01-12'))
But I am lost when it comes to steps 2 and 3... Can someone help me out?
To find out which Wednesday of the month it is, you can do this using the day of the month
(int)(day_of_month / 7) + (day_of_month % 7 == 0 ? 0 : 1)
Thus, for 2012-01-12 you'll get:
(int)(12 / 7) + (12 % 7 == 0 ? 0 : 1) = 1 + 1 = 2 -> second Wednesday

find date months ago, will it miss dates if current month is shorter?

I am writing a script to query dates "3 months ago". As I am thinking about making sure I do not miss anything, I am using this for comparison:
$date = date("Y-m-d",mktime(0,0,0,date("m")-$months_ago,date("d")));
I am realizing there is a chance I could be missing dates. For example, if I search on 11/30 for dates three months ago, I will get 8/30. But the next day is 12/1 and three months prior is 9/1. So in this script I have missed 8/31.
I guess the best method is to use days (90 days) instead on months. Is this the best practice for something like this?
Go directly with strtotime('-3 month'); you can also give negatives like -3 on the month param of the mktime and it will work like charm (better the second solution). No, it wont skip days - if "now" is 2011-06-17 it would return timestamp equivalent to 2011-03-17.
Edit:
Well, it might actually be true that you can miss days (I haven't checked your statement) but after all your unit of measurement of time is months, not days. What I'm saying is that in the Gregorian calendar month isn't constant amount of time - it could be 28, 29, 30 or 31 days.
Let's say you want to calculate months for a paid subscription period. If the user pays one month on 2011-02-15, when would his subscription expire? I would guess 2011-03-15, even though there are just 28 days between those two dates And if he pays for subscription on 2011-03-15, he would get full 31 days till 2011-04-15 and this seems perfectly fair to me as the subscription is "one month", which just happens to be different amount of days through the year.
If, in your case you don't want to get "3 months ago" but want to get constant amount of time that relatively represents "3 months", then you can use the medium month length - 88.59 days, or 88 days 14 hours and 10 minutes. That represented with code would be:
strtotime('-88 days -14 hours -10 minutes');
$when = strtotime('-3 months');
If you just need the month/year and don't want to have to calculate days:
$m = 5; // how many months ago, for example
$now = time();
$cm = date("m",$now); // current month
$yr = date("Y",$now) - intval((12 + $m - $cm)/12);
$month = (($cm + 11 - ($m % 12) ) % 12) + 1;
echo "$m months ago: $month yr: $yr\n";

Categories