Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    Proper date range format?

    Hi
    This works because I'm working with a single number, problem is I need to do
    this with a month and day like between 11-19 and 12-15 etc, should I use a
    timestamp or z for day of year? whats the most efficient way to do something
    like this?.

    PHP Code:
        if (date("m") >= && date("m") <= 11) {
           echo 
    "This is within range";
           echo 
    date("m");
        }
        else {
           echo 
    "Not within range";
           echo 
    date("m");
        }

    // I need to do something like

        
    if (date("m-d") >= "11-19" && date("m-d") <= "12-15") {
           echo 
    "This is within range";
           echo 
    date("m-d");
        } 
    Thanks
    Sonny
    Last edited by sonny; 11-19-2011 at 05:19 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Since you're not concerned about "Year",
    are you going to always assume it's between the first date and second date,
    not the second and first date? There is a difference.

    So if you have this:

    11-19 ... 02-20 (you mean 11-19-2011 and 02-20-2012)

    and not (11-19-2011 and 02-20-2011)?

    If you're always assuming the 2nd date is further in the future than the first date,
    you can use UNIX timestamp. Otherwise, the script would never work ... not knowing
    which direction you're going. Do you see what I mean?

    My test ... upload this and see what it does ...
    PHP Code:
    <?php

    // some test values.
    // in this example, the 2nd date is assumed to be 2012, not 2011.
    $date1="11-17";
    $date2="02-15";

    // the UNIX timestamp right now.
    $now=time();

    // determine which year to use.
    if(strtotime(date("Y")."-".$date1) < strtotime(date("Y")."-".$date2)){
    $date1=strtotime(date("Y")."-".$date1);
    $date2=strtotime(date("Y")."-".$date2);
    }
    else{
    $date1=strtotime(date("Y")."-".$date1);
    $date2=strtotime((date("Y")+1)."-".$date2);
    }

    if(
    $now >= $date1 && $now <= $date2){
    echo 
    "Within range";
    }
    else{
    echo 
    "Outside of range";
    }

    ?>

    .

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    I figured it out, in case anyone needs something like this.

    PHP Code:
        if ( time() >= strtotime('2011-11-18') && time() <= strtotime('2011-11-27')) {
           echo 
    "<p>Have a nice thanksgiving</p>";
        }
    //elseif (do other dates etc if needed 

    Sonny

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    As you can see, specifying the year makes it 98% easier.


  •  

    Posting Permissions

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