www.webdeveloper.com
Results 1 to 2 of 2

Thread: Find Last Occurrence of Date?

  1. #1
    Join Date
    Jan 2003
    Posts
    218

    Wink Find Last Occurrence of Date?

    There has got to be a better/simpler solution for this than what I came up with. How can I use PHP to find the date for the last occurrence of September 30th? I'm then going to use it in a MySQL search query. Thanks for your help.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,252
    Best I could think of on the spur of the moment:
    PHP Code:
    <?php
    /**
     * Get the most recent September 30th as a UNIX timestamp
     * @return int
     * @param int $now (optional timestamp for testing)
     */
    function getLastSep30($now=null)
    {
       if(!
    $now) {
          
    $now time();
       }
       
    $yearNow date('Y'$now);
       
    $monthNow date('n'$now);
       
    $lastSep30ts = ($monthNow 9)
                    ? 
    mktime(0,0,0,9,30,$yearNow)
                    : 
    mktime(0,0,0,9,30,$yearNow 1);
       return
    $lastSep30ts;
    }
    // normal usage
    echo date('Y-m-d'getLastSep30()) . "<br />\n";
    // tests of boundary conditions:
    echo date('Y-m-d'getLastSep30(strtotime('2010-10-01'))) . "<br />\n";
    echo 
    date('Y-m-d'getLastSep30(strtotime('2010-09-30'))) . "<br />\n";
    echo 
    date('Y-m-d'getLastSep30(strtotime('2010-09-29')));
    Output:
    Code:
    2010-09-30
    2010-09-30
    2009-09-30
    2009-09-30
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles