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 1441
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,20 05] 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($thisDayOfMo nth != "*" && 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 the position of the
second colon. How do I go about that?? I can only find functions that
return the position of the first or last occurence, but not a variable
occurence ::sniffles::
|
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 subsequently found that out of the three possible ways (that I know
of) of declaring repeating elements in xsd, only one of these was
understood:
|
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 textdocument "behaviour in school" which is only valable
|
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 some hesitations about .NET Framework occurence on computers of
such users. Is there any statistics regarding this problem? I'm intrested
mostly in statistics for USA and European Union.
|
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 find a way of closing this Hidden occurrence. I can
close any foreground instances without any problem, but the same
coding does not work for hidden occurences.
| |
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 only one
occurence in the table.
I tried the group by and within the having clause I placed the
count(myfield) = 1, did not work...
|
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 tried sth and it is working
but I could not structure my algorithm very well so I had to add some
more lines after thinking it on a paper draft. I am posting the whole
code and waiting different ideas.
First I sorted them with the sort function,...
|
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 occurence if it is available otherwise should display "NOT FOUND".
|
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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |