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
Related
I know an event happens on a Monday in any given year. I also know that the event falls between May 25-31 of any given year. If I calculate the type of day May 25th is, I find out that its a Wednesday. But how can I write a script to determine the event will be May 30th?
I'm assigning the days a number: Sunday = 1, Monday = 2, etc...
I have variable A equal to what day May 25th is: A = 4
I have variable B equal to what day the event occurs on: B = 2
I'm setting X equal to how many days I need to add to A to have the correct date.
I wrote this equation that seems to work:
(A + X) % 7 = B
(4 + X) % 7 = 2
I know if I set 5 as X the equation is true, and 5 is how many days I need to add to May 25th to make it May 30th, but how can I rewrite the equation so it is equal to X and not to B?
Im currently programming a calendar with php and mysql and im stuck with some functionality while selecting all events i want to display. I implemented the possibilty to repeat an event. Therefore i set a timestamp from which the event starts and a timestamp to determine when the event ends. Furthermore i got some integer values which represent the rythm in which the event is repeated.
Then i fetch the events based on a timestamp which is send with a request.
I now want to enable the user to shift events from the weekend to the Friday before the weekend or the monday after the weekend. For example:
From: 1450306800 (today)
until: 0 (infinite)
rythm : 1 (-> every month)
jump:2 (-> on every 2nd day / month)
weekends : 3 (-> shift to next monday)
-> January 2nd 2016 is a saturday and i want to display that event on the next monday.
currently my select looks something like this: (:day -> timestamp from request, :d -> day of month from :day, :weekday -> day of the week from :day)
SELECT * FROM events
WHERE repeat_from <= :day
AND ((repeat_until >= :day) OR (repeat_until = 0))
AND CASE weekends
WHEN 0 THEN (:weekday BETWEEN 1 AND 7)
WHEN 1 THEN (:weekday < 6)
WHEN 2 THEN ??
WHEN 3 THEN ??
AND CASE rythm
WHEN 1 THEN (:d - DAY(FROM_UNIXTIME(repeat_from))) / (jump + 1) = CEIL ((:d - DAY(FROM_UNIXTIME(repeat_from))) / (jump + 1)) ... [all the other cases]
How do i check if the event would have been displayed on saturday or sunday before/after within the select? The only way i can think of is to more or less repeat the whole select from the "...AND CASE rythm..." part which is quite alot.
Or would the best way be, to fetch the event on every monday/friday anyway if it shifts and then check with a php function if the event would have been displayed on saturday or sunday ?
Why don't you do all the calculations and matching on the PHP back-end? With that much cases and clauses in your MySQL query you'll probably get yourself a really slow execution.
Select all Events by your major criteria. In your case that would probably be:
SELECT * FROM
events
WHERE
repeat_from <= :day AND
(
(repeat_until >= :day) OR
(repeat_until = 0)
)
Then just loop through all fetched rows and apply the built-in date() method - much faster and flexible:
# you can match the week number of different dates
$currentWeekNumber = date('W');
$specificDayWeekNumber = date('W', strtotime($specificDate));
# or match the day number
$currentDayNumber = date('N');
$specificDayNumber = date('N', strtotime($specificDate));
The date() method is super flexible and will allow you to check if certain Event date is within the weekend or not, so you can manipulate it further.
If you provide some further explanations and/or examples, I'll try to be more specific with the solution. Cheers.
I need to calculate the number of the week following a given week. This is trivial for most weeks within the year, but not at the end / beginning of the year. For example if I need to know the week following week 52 of 2013.
I would like to use the defaul_week_format 3, i.e.
a) the weeks start with Monday (instead of Sunday)
b) the numbering within a year starts with 1 (instead of 0)
and c) week number 1 is the first week with more than 3 days this year.
My approach to e.g. calculate the week after week 36 2014 is the following:
SELECT WEEK(DATE_ADD(STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'), INTERVAL 1 WEEK))
For this example I calculate the date of Tuesday in week 36, add one week, and calculate the week of this day. This would work, but even without adding one week, I have this strange behaviour:
It works for the default_week_format 0:
SET ##local.default_week_format=0;
SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W');
-> Tuesday of week 36 is 2014-09-09
SELECT week('2014-09-09');
-> 2014-09-09 is in week 36
So far, so good: Tuesday of week 36 is in week 36.
But this simple experiment does NOT work for default_week_format 3:
SET ##local.default_week_format=3;
SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W');
-> Tuesday of week 36 is 2014-09-09
-> This is wrong, because numbering should start with 1 (instead of 0 now)!
-> Tuesday of week 36 should be 2014-09-02
SELECT week('2014-09-09');
-> 2014-09-09 is in week 37
So MySQL tells me that Tuesday of week 36 is in week 37!
My only explanation for this:
week() takes default_week_format into account
str_to_date() ignores the default_week_format
How can I force MySQL function str_to_date to use the default_week_format?
Or: How else can I calculate the number of the following week?
I could also do it in PHP, but I get the weeks' numbers from a MySQL database.
Thanks for any help!
I was facing exactly the same problem. My solution - perhaps not the greatest one, but it works.
As you already mentionned: Most of the cases are trivial, you just need to know the year end / beginning. Therefore, I just store the relevant weeks for the needed number of years in the following table:
CREATE TABLE `NEXT_WEEK_TAB` (
`CUR_WEEK` varchar(8) NOT NULL,
`NEXT_WEEK` varchar(8) NOT NULL,
PRIMARY KEY (`CUR_WEEK`,`NEXT_WEEK`)
)
You get the following week now with the current algorithm:
following_week = (SELECT next_week
FROM next_week_tab
WHERE cur_week = <your_current_week>);
if !following_week then following_week++;
As I said: Probably not the nicest solution, but it's pretty simple - and it works.
I think you must use Week with a mode argument
mysql> SELECT WEEK('2008-02-20',0);
-> 7
mysql> SELECT WEEK('2008-02-20',1);
-> 8
take a look here :
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
I have a SQL statement set up to grab data from the current calendar week. By default, this grabs data starting on Sunday and ending on Saturday. I was hoping to change this so that the start of the calendar week is Thursday and it ends on Wednesday. Below is my statement:
SELECT *
FROM transactions
WHERE yearweek(transactionDate) = yearweek(now())
ORDER BY transactionDate DESC
Is there a way to modify the yearweek in a way to allow this?
Thanks!
You can take advantage of WEEKDAY() which returns a number representing the day of the week (0 = Monday, 6 = Sunday) and some straightforward maths to rewrite this query.
Subtract the weekday you want the week to start on (in your case 4 = Thursday) from the selected date, add 7 and take the remainder from 7. This will give you the number of days to subtract to get the start of your range.
A similar logic applies to calculate the end date of the range.
SELECT *
FROM transactions
WHERE DATE(transactionDate)
BETWEEN DATE_SUB(DATE(NOW()), INTERVAL (WEEKDAY(NOW()) - 4 + 7) % 7 DAY)
AND DATE_ADD(DATE(NOW()), INTERVAL 6 - (WEEKDAY(NOW()) - 4 + 7) % 7 DAY)
ORDER BY transactionDate DESC;
For a different starting date, substitute the weekday for 4 in the query.
You can specify start day - default is 0 (Sunday).
Just set it to 4 (Thursday):
YEARWEEK(transactionDate, 4)
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.