Results 1 to 4 of 4

Thread: Working with dates

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default Working with dates

    How would I test for a range of dates, regardless of the year?

    For example:

    If ( between April 1st and November 1st )
    DO THIS
    else
    DO THIS

    I just realized, maybe:

    PHP Code:
    $month date(m);
    if ( 
    $month >= && $month <= 10 )
       
    // Do this
    else
       
    // Do this 
    Maybe writing it out helped.

    Is there a better way of doing it?


    Thanks

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Did that work like that? I thought that the date was a string, which wouldn't be comparable with an integer.

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    [PHP
    ]$month = date(m);
    if ( $month >= 4 && $month <= 10 )
    echo "It is between April and November.";
    else
    echo "It isn't between April and November.";
    [/PHP]

    Yeah, the above works perfectly. date() does return a string, but depending on what context the string is used in, PHP knows how to handle it. In this case comparing it to numerical values. This is why you'll hear or read that PHP is a "loosely typed" language. The only time you'll have problems is if you try to use a string in a function that explicitly requires an integer.

  4. #4
    Join Date
    Feb 2009
    Posts
    73
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    maybe this will work :

    Code:
    $date1 = date("d m Y");
    //$date2 fetch date from database (ex : creation date)
    
    //explode date1 and date2 :
    $temp1 = explode(" ", $date1);
    $month1 = $temp1[1]; //the 2nd array
    
    $temp2 = explode(" ", $date2);
    $month2 = $temp2[1]; //the 2nd array
    
    //compare them :
    if($month1 >= $month2){
      echo "You Create Programs After Today";
    }
    else{
      echo "You Create Programs Before Today";
    }
    I haven't tried this code, CMIIW, hope that I can help you

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
  •