Web Analytics Made Easy -
StatCounter comparing a date to todays date - CodingForum

Announcement

Collapse
No announcement yet.

comparing a date to todays date

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • comparing a date to todays date

    I have had a look at the date() bit on php.net but still dont really understand how to do it - I have a date in the form yyyy-mm-dd and want to do a check to see if it is less than todays date or not. Can anyone help? Cheers.

  • #2
    use to strtotime ($date); reformat your yyyy-mm-dd string into Unix time (or seconds since the epoch). then compare that value to the value returned from time(); like this:

    PHP Code:
    $date "2004-02-29";
    $unix_date strtotime ($date);

    if (
    $unix_date time() ) { echo "date is before"; }
    else { echo 
    "date is after"; } 
    if date is same as today's date, then it returns before.
    "There is more than one way to do it."

    Comment


    • #3
      Quality! Thanks - big help!!

      Comment


      • #4
        Originally posted by dswimboy

        if date is same as today's date, then it returns before.
        is there a way to return 'after' if it is todays date, rather than before?

        Thanks.

        Comment


        • #5
          [PHP]$date = "2004-02-29";
          $unix_date = strtotime ($date);

          if ($unix_date <= time() ) { echo "date is before"; }
          else { echo "date is after"; }
          [PHP]

          adding the equals sign should work. you'll have to test it.
          "There is more than one way to do it."

          Comment


          • #6
            Just double checked that and it doesnt work (even though I thought it did for a minute!) so I am still struggling if anyone can help?
            Last edited by heaps21; Mar 3, 2004, 09:59 AM.

            Comment


            • #7
              I think the problem is that strtotime() considers the time as well as the date, whereas teh date i want to compare is just a date with no time.

              My next question then, is if time() returns for example 1078326922 is there some part of that value that specifically refers to today (the 1st 4 values for example)? I just need to be sure before I implement a comparison based on a substring of the value.

              Cheers.

              Comment


              • #8
                PHP Code:
                $date "2004-02-06";
                $u_date strtotime ($date);

                if (
                $u_date == strtotime(date("Y-m-d")) ) { echo "date is today"; }
                elseif (
                $u_date strtotime(date("Y-m-d")) ) { echo "date is before today"; }
                else { echo 
                "date is after today"; } 
                strtotime(date("Y-m-d")) returns todays date in seconds since the epoch. date("Y-m-d") returns todays date in the form of "yyyy-mm-dd".
                "There is more than one way to do it."

                Comment

                Working...
                X