Hi,
Interesting question:
A user fills a form about how often an event happen.
He has 4 fields: [Day of the week, Day of the month, Month, Year].
In any of them, he can enter "*" as a joker, to say it doesn't matter.
For example, his birthday is every October 5th, so he enters:
[*,5,10,*], because the day of the week, or the year don't matter.
He has an apointment every Monday:
[Monday,*,*,*]
He has to pay the rent first day of each month in 2005:
[*,1,*,2005]
You get the situation?
Now once I've collected the data, I want to know WHEN is the next time
this event will happen.
How do I do that?
I've tried to play around with strtotime(), but it doesn't work for
everything.
Please help me to find a way to solve this problem.
Thank You,
Nathan 3 1427
Hmmm...let's see. I'll propose a relatively simple solution, although
it certainly isn't on the cutting edge of efficiency. Suppose we have:
$dayOfWeek; //Monday, Tuesday, etc.
$dayOfMonth;
$month;
$year;
Then we can just keep adding one day at a time until we find the next
match:
$i = 1; //set to 0 if you want to include today
while(true) {
$thisTime = strtotime("+" . $i . " day"); //$i days from now
if($year != "*" && date("Y", $thisTime) != $year)
continue;
if($month != "*" && date("n", $thisTime) != $month)
continue;
if($dayOfMonth != "*" && date("m", $thisTime) != $dayOfMonth)
continue;
if($dayOfWeek != "*" && date("l", $thisTime) != $dayOfWeek)
continue;
return $thisTime; //UNIX timestamp of the next event occurence
}
You should also include some check in the while loop to avoid an
infinite loop (imagine a situation where you specify all four
parameters and they represent a date that doesn't exist, i.e.
[Tuesday,27,3,2005] or the case where they enter a date in the past).
As I said, it's horribly inefficient in terms of performance, but
reasonably efficient in terms of coding. This way you avoid all the
crazy cases and various combinations of wildcards.
Cool that's a very useful shortcut.
2 little mistakes though:
- Need to increment $i .
- The day of the the month is "j" not "m".
Now add a little trick, I'd like the user to be able to enter "last"
instead of a number in $dayOfMonth. So that it doesn't matter how many
days there are in the month.
Yeah, my oversight. Correcting my mistakes, and then adding the
ability to specify the $dayOfMonth as "last" as well:
$i = 0; //set to -1 if you want to include today
while(true) {
$i++;
$thisTime = strtotime("+" . $i . " day"); //$i days from now
$thisDayOfMonth = $dayOfMonth;
if($dayOfMonth != "*" && $dayOfMonth == "last")
$thisDayOfMonth = date("t", $thisTime);
if($year != "*" && date("Y", $thisTime) != $year)
continue;
if($month != "*" && date("n", $thisTime) != $month)
continue;
if($thisDayOfMonth != "*" && date("j", $thisTime) !=
$thisDayOfMonth)
continue;
if($dayOfWeek != "*" && date("l", $thisTime) != $dayOfWeek)
continue;
return $thisTime; //UNIX timestamp of the next event occurence
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tesla |
last post by:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :"
Is there a function to find the position of the "I"th occurence of a
character/string? Like lets say I want to find out...
|
by: Roland |
last post by:
hi all
having designed my schema to use repeating groups of elements, I found
that some applications (eg. Microsoft InfoPath) refused to recognised
that element as a repeating element.
I...
|
by: Armin Wagenknecht |
last post by:
Hello,
i am modelling a TopicMap, and I have the following problem:
I want to use two scopes in the topicmap for one occurence which has
to satsify BOTH scopes!!
Example:
I have the...
|
by: truetype |
last post by:
Greetings!
I consider to create an application using .NET Framework. It would be used
by any kind user, mostly those who do not know anyting about programming and
..NET Framework.
But have...
|
by: Gary Cobden |
last post by:
Hi
I have a routine that uses VBA to open a hidden occurence of Excel,
and do background computations.
However, in the event that the routine terminates abnormally, I have
not been able to...
|
by: das |
last post by:
Hi all,
How can I get a row that has only one occurence in a table? Not
through 'distinct' because this gets a single row that might have
multiple occurences, I want to get only rows that have...
|
by: utab |
last post by:
Dear all,
I tried to solve the occurence problem: to find the distinct occurences
of a word in an input. I know that I could use map and other STD lib
functions. I tried to do it the hard way. I...
|
by: gauravguleria |
last post by:
wht's the program which takes two strings using command line arguments and finds the occurence of second string in the first string.The program should return the starting position of first...
|
by: mustafalp |
last post by:
i want to write it with following prototype:
int occurence (int a,intasize,int e)
this function must returns the number of occurence of e in the array a in c++.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |