I need a php function that will return the date for an event that occurs on a set time frame (e.g. Last Saturday in May).
Anybody have such a function?
I need a php function that will return the date for an event that occurs on a set time frame (e.g. Last Saturday in May).
Anybody have such a function?
It's possible to make one. But not from input text of "Last Saturday in May". What are the parameters?
My guess is that this may require some searching (loops with if statements, for example).
On the other hand, there may be a better way to do what you're doing (for example, if you're designing a calendar, there are simpler ways to find the number of weeks/rows in the month, just using loops).
The complexity of what is required will depend on the complexity and variability of the input. Last Saturday in May, for example, would simply require finding the length of May, then looping backwards and checking whether that date is a Saturday.
Playing around with this will help-- try converting between string dates and timestamps (strtotime), and then manipulating those timestamps (basic math), then converting those to readable dates (date).
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
The requirement is very unique and specific. I am designing a site for my high school alumni association. The oldest active alumni association in the state of Oklahoma. Began in 1903.
Anyway they have their annual banquet each year the last Saturday in May. On the website they want to have information concerning the banquet but it does not need to be posted until April 1st each year and will become old info after the banquet.
To solve the problem of out dated material I thought I would do some coding to simply not display the menu link to the information if the current date was before April 1st or after the banquet date.
I'll get it figured out. Just like to work smart not hard.
jdadwilson
Oh, so you only need that one specific date? That's no problem-- you don't need a function, just some math. (It would get complicated if you need this for the 4th Tuesday of October, the first Wednesday in January, and the first business day after Christmas, etc., but one date makes things much simpler.)
1. Find a reference point. This will be relative to the current year. So assuming you don't need to have this before/after the current year, then you can just use date('Y') to get the year. Then you need to know the month-- that's May. But instead, since you want the last Saturday of the month, let's start with June and work backwards. Now you can get the timestamp for the first day of June:
$d = strtotime('June '.date('Y'));
2. Now you need to find the last Saturday before June (in May). Note that "one day" in seconds is 60*60*24 = 86400. I'll do this with a subtracting loop:
I hope that helps!PHP Code:$d = $d-86400; //skip first day-- it's in June
while(date('l',$d)!='Saturday') {
$d=$d-86400;
}
echo $d; //last Saturday in May (technically, the first second of that day, but you can ignore it)
echo date('n/j/y',$d); //the date as in 5/30/13 or whatever
//you can also use $d now to compare to the current date to determine if it's before or after that date, as needed
EDIT: now that I've posted that, there may be a much simpler option. strtotime() isn't particularly well documented, so I'm not sure what would be most efficient. But it probably can be used for this. You could try even just:
strtotime('last Saturday in May')
But I doubt that complex an idea would work. Instead, I think this will:
strtotime('last Saturday',strtotime('June '.date('Y')))
That is, relative to June YYYY, when was the last Saturday?
I haven't tested it, but I guess that would work fine.
Last edited by djr33; 07-21-2013 at 12:22 AM.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
jdadwilson (07-21-2013)
djr33... you rock.
Thanks...
Bookmarks