PHP Function - check if date has passed

7 replies
Hi,

My PHP programming skills are very rusty at the moment and I am hoping I could get some help here from fellow warriors.

I have an HTML page that shows events for September (presented by date in ASC order). I want the page to automatically hide events for dates that are in the past. The function would be called upon multiple times throughout the page (about 30, once for each day) and will retrieve a single variable (date)

For example, I want the function to hide MONDAY, SEP 12 (yesterday) but show TUESDAY, SEP 13 (today) as well as WEDNESDAY, SEP 14 (tomorrow)



MONDAY, SEP 12
Moon Festival (Mid-Autumn Festival)


TUESDAY, SEP 13
Thievery Corporation (5:30pm)


WEDNESDAY, SEP 14
Canada vs. Tonga - Rugby World Cup 2011 (9:30am)



Thank you very very much in advance!!!
#check #date #function #passed #php
  • Profile picture of the author Unomateo
    This will show the next 7 days

    for($i = 0; $i < 7; $i++)
    {
    $date = mktime(0, 0, 0, date("m"), date("d")+$i, date("Y"));
    echo date("F j, Y, D", $date)."<br>";
    }
    {{ DiscussionBoard.errors[4671941].message }}
    • Profile picture of the author osegoly
      Thanks Unomateo. I am actually looking for a function that I can call up with a specific date (e.g. '9/13/2011'), the function would then determine whether the date is in the past and if not, it would return a True value. If the date is in the past, it would return False.

      Does that make sense?

      The page it is going to be on has events for each day of the month. So I'd be calling that function 29-30 times on the page (depending on which month it is).
      {{ DiscussionBoard.errors[4672887].message }}
  • Profile picture of the author mywebwork
    This might be the type of thing you need:

    Comparing Dates in PHP

    It's not a function, but shows the basis of doing a date comparison and would be simple to turn into a function.

    Just remember, anything running on PHP will run on your servers time, which is often GMT. So if your visitors are mostly on the west coast you'll be off by 8-9 hours (depending upon the time of the year). You could compensate for that by subtracting a few hours from the call to today's current time.

    Bill
    {{ DiscussionBoard.errors[4673369].message }}
  • Profile picture of the author michael_gourlay
    Where is the data coming from? If it's coming from a DB, can you edit your SQL query to remove dates in the past?

    Otherwise, you should check out the link mywebwork posted.
    {{ DiscussionBoard.errors[4675619].message }}
  • Profile picture of the author Unomateo
    ok, here you go date('z') will give you the day of year 0-365. therefore you can just take today and then see if the date you pass is less then or greater than to see if it's in the past.

    // get the day of today
    $today = date('z');

    // you can pass any date to the strtotime function
    $day = date('z', strtotime("2011-09-01"));

    // check to see if the date has passed
    echo ($day < $today)?"Date has passed":"date in the future";
    {{ DiscussionBoard.errors[4676862].message }}
    • Profile picture of the author obiking
      Let me get you right, you are going to pass any date to the function and the function will check against current date to determine if it is in the past or not.

      or

      You are going to pass two dates as a parameter to the function and the function will determine if the first date parameter is in the past or not.
      {{ DiscussionBoard.errors[4676956].message }}
    • Profile picture of the author BrianLeanza
      Probably the best way to compare dates is by looking at their timestamp (aka ticks, if you are coming from a Microsoft backgrounds).

      A timestamp (or tick) is the number of seconds since January 1st 1970 (in PHP).

      The function to get that number for any given date is : strtotime

      With that knowledge in hand the task is really easy

      // compares any date to the current date
      function isInThePast(aDate)
      {
      return strtotime("now") > strtotime(aDate);
      }

      alternatively, if you want to compare against another date instead of 'now':

      function isInThePast2(aDate, aDateToCompareTo)
      {
      return strtotime(aDateToCompareTo) > strtotime(aDate);
      }

      ********************

      Moreover, if you are using the DateTime class (http://de.php.net/DateTime) things are even easier, since objects of the type DateTime will understand comparison operators

      aDateTime1 > aDateTime2 ... etc will work perfectly fine.

      ********************
      Cheers,

      Brian
      {{ DiscussionBoard.errors[4677000].message }}

Trending Topics