Results 1 to 5 of 5

Thread: Need Reverse Date Function

  1. #1
    Join Date
    Feb 2013
    Location
    California
    Posts
    53
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Need Reverse Date 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?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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

  3. #3
    Join Date
    Feb 2013
    Location
    California
    Posts
    53
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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:
    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 
    I hope that helps!




    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

  5. The Following User Says Thank You to djr33 For This Useful Post:

    jdadwilson (07-21-2013)

  6. #5
    Join Date
    Feb 2013
    Location
    California
    Posts
    53
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    djr33... you rock.

    Thanks...

Similar Threads

  1. Problem With Date() and If Function
    By M2com in forum PHP
    Replies: 1
    Last Post: 08-15-2011, 02:12 AM
  2. php date(1) function
    By autofocus in forum PHP
    Replies: 1
    Last Post: 11-24-2009, 05:50 AM
  3. Resolved PHP date() function, what's happening?
    By xiofire in forum PHP
    Replies: 5
    Last Post: 05-12-2009, 10:15 PM
  4. how to display only tuesday date using date function
    By meenakshi in forum JavaScript
    Replies: 15
    Last Post: 06-01-2008, 11:16 PM
  5. the date function for GMT?
    By Dennis_Gull in forum PHP
    Replies: 2
    Last Post: 04-21-2007, 11:08 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •