www.webdeveloper.com
Results 1 to 11 of 11

Thread: check between dates

  1. #1
    Join Date
    Jun 2004
    Posts
    1,299

    check between dates

    Hi,

    I have search because i thought it was common but I cannot find what i need.

    I have a $startdate, $enddate and $date and they are formated "dd/mm/yyyy"

    I need to check if $date is between $startdate and $enddate?

    Im sure its simple but I have tried so many things to no avail.

    Thanks
    k0r54

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    change the date into a timestamp. You could use strtotime.

  3. #3
    Join Date
    Jun 2004
    Posts
    1,299
    Hi,

    I have tried that but when i do
    PHP Code:
    echo $_POST[$requiredfield] . ",";
    echo 
    $startdate ",";
    echo 
    $enddate ";";
    echo 
    strtotime($_POST[$requiredfield]) . ",";
    echo 
    strtotime($startdate) . ",";
    echo 
    strtotime($enddate) . ";"
    is outputs
    PHP Code:
    10/12/2001,01/01/1900,27/7/1991;1002841200,,; 
    It cannot seem to convert the start and end date

  4. #4
    Join Date
    Jun 2004
    Posts
    1,299
    Ok i think its got something to do with the uk vs us date below is the whole code: -
    Code:
    <input type="hidden" name="DTBTW_RQ_dob_D" value="01/01/1900,today,17,year,-" />
    <input type="text" name="RQ_dob_D" maxlength="10" value="<?PHP echo $_POST['RQ_dob_D']; ?>" /> dd/mm/yyyy
    PHP Code:
    // Check if a date field has a between validation
        
    if (substr($key6) == "DTBTW_") {
          
    //explode the value to get the dates between and get the field name
          
    $datevar explode(","$val);
          
    $requiredfield substr($key6strlen($key));
          
          
    // work out the start and end dates and convert them to string
          
    $startdate $datevar[0];
          
          
    // Work out if the end date is today or a fixed date
          
    if ($datevar[1] == "today") {
            
    $enddate date("j/n/Y");
          } else {
            
    $enddate $datevar[1];
          }
          
          
    // Now work out if we need to substract any dates from the end date
          
    $enddateArr explode("/",$enddate);
          if (
    $datevar[4] == "-") {
            if (
    $datevar[3] == "day") {
              
    $enddate date("j/n/Y"mktime(0,0,0$enddateArr[1], ($enddateArr[0]-$datevar[2]), $enddateArr[2]));
            } elseif (
    $datevar[3] == "month") {
              
    $enddate date("j/n/Y"mktime(0,0,0$enddateArr[1]-$datevar[2], ($enddateArr[0]), $enddateArr[2]));      
            } elseif (
    $datevar[3] == "year") {
              
    $enddate date("j/n/Y"mktime(0,0,0$enddateArr[1], ($enddateArr[0]), $enddateArr[2]-$datevar[2]));      
            }
          } elseif (
    $datevar[4] == "+") {
            if (
    $datevar[3] == "day") {
              
    $enddate date("j/n/Y"mktime(0,0,0$enddateArr[1], ($enddateArr[0]+$datevar[2]), $enddateArr[2]));
            } elseif (
    $datevar[3] == "month") {
              
    $enddate date("j/n/Y"mktime(0,0,0$enddateArr[1]+$datevar[2], ($enddateArr[0]), $enddateArr[2]));      
            } elseif (
    $datevar[3] == "year") {
              
    $enddate date("j/n/Y"mktime(0,0,0$enddateArr[1], ($enddateArr[0]), $enddateArr[2]+$datevar[2]));      
            }      
          }    
          
    // TEST
          
    echo $_POST[$requiredfield] . ",";
          echo 
    $startdate ",";
          echo 
    $enddate ";";
          
          echo 
    strtotime($_POST[$requiredfield]) . ",";
          echo 
    strtotime($startdate) . ",";
          echo 
    strtotime($enddate) . ";";
    // END TEST

          // Now check that the date in the date field is between startdate and enddate THIS IS NOT CORRECT STILL WORKING IT OUT
          
    if((time($_POST[$requiredfield]) < time($startdate)) && (time($_POST[$requiredfield]) < time($enddate))) {
            
    $errormsg[$requiredfield] = "<span class=\"VAL_error\">This entry must be between " $stardate " and " $enddate ".</span>";
          }
        } 
    If i enter in the form my date in uk style then it cannot convert that either.

    Any ideas?

    Thanks
    k0r54
    Last edited by k0r54; 07-27-2008 at 12:34 PM.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    I strongly recommend that you have the form use separate fields for month, day, and year. They can each be a select elements or text input elements, whichever you prefer, but this will get rid of any possible ambiguity as to what format is being used. You can then just use them in a mktime() to get the UNIX timestamp integer.
    PHP Code:
    $month = (int)$_POST['month'];
    $day = (int)$_POST['day'];
    $year = (int)$_POST['year'];
    if(
    checkdate($month$day$year) == false)
    {
       
    // invalid date, have user re-enter
    }
    else
    {
       
    $timestamp mktime(000$month$day$year);

    "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

  6. #6
    Join Date
    Jun 2004
    Posts
    1,299
    Hi,

    This code is to validate that the date they select is between the startdate and enddate I tell it. How the user inputs it fairly irelevant at this point. Although I will say I have taken it on board and have done it but I still have this problem.

    Thanks for you time
    k0r54

  7. #7
    Join Date
    Jun 2004
    Posts
    1,299
    Hi

    The endtime works great but the start time is

    Code:
    $startdateArr = explode("/",$datevar[0]);
    echo mktime(0,0,0,$startdateArr[1],$startdateArr[0],$startdateArr[2]);
    $datevar[0] = "14/02/1900";

    just just echos blank. It does not seem to work even if i physicly type 14, 03, 1900 to replace the variable it still dont work.

    ??? bizzare?

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    I don't think UNIX timestamps will go back as far as 1900. Assuming timestamps are being represented as signed 32-bit integers, positive values go from 1970-01-01 to some time in 2038 (after which we all better be using 64-bit representations). If I correctly did the math in my head, the farthest back you can go with a negative value would be some time around 1902.
    "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

  9. #9
    Join Date
    Jun 2004
    Posts
    1,299
    very impressive nogdog! Never let me down!1902 is indeed correct 1901 failed but 1902 worked!

    Thanks for your help

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    PS: If you have lots of "date arithmetic" challenges or need to deal with a wider range of dates than supported by the standard date/time functions, you might want to look into the PEAR Date package for enhanced capabilities.
    "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

  11. #11
    Join Date
    Jun 2004
    Posts
    1,299
    Just for the record the final working code is :-

    PHP Code:
        // Check if a date field has a between validation
        
    if (substr($key6) == "DTBTW_") {
          
    //explode the value to get the dates between and get the field name
          
    $datevar explode(","$val);
          
    $requiredfield substr($key6strlen($key));
                
          
    // Work out if the end date is today or a fixed date
          
    if ($datevar[0] == "today") {        
            
    $startdateArr explode("/",date("j/n/Y"));
          } else {
            
    $startdateArr explode("/",$datevar[0]);
          }
          
          
    $startdate mktime(0,0,0$startdateArr[1], $startdateArr[0], $startdateArr[2]);
          
          
    // Work out if the end date is today or a fixed date
          
    if ($datevar[1] == "today") {        
            
    $enddateArr explode("/",date("j/n/Y"));
          } else {
            
    $enddateArr explode("/",$datevar[1]);
          }      
          
          
    // Now work out if we need to substract any dates from the end date
          
    if ($datevar[4] == "-") {
            if (
    $datevar[3] == "day") {
              
    $enddate mktime(0,0,0$enddateArr[1], ($enddateArr[0]-$datevar[2]), $enddateArr[2]);
            } elseif (
    $datevar[3] == "month") {
              
    $enddate mktime(0,0,0$enddateArr[1]-$datevar[2], ($enddateArr[0]), $enddateArr[2]);      
            } elseif (
    $datevar[3] == "year") {
              
    $enddate mktime(0,0,0$enddateArr[1], ($enddateArr[0]), $enddateArr[2]-$datevar[2]);      
            }
          } elseif (
    $datevar[4] == "+") {
            if (
    $datevar[3] == "day") {
              
    $enddate mktime(0,0,0$enddateArr[1], ($enddateArr[0]+$datevar[2]), $enddateArr[2]);
            } elseif (
    $datevar[3] == "month") {
              
    $enddate mktime(0,0,0$enddateArr[1]+$datevar[2], ($enddateArr[0]), $enddateArr[2]);      
            } elseif (
    $datevar[3] == "year") {
              
    $enddate mktime(0,0,0$enddateArr[1], ($enddateArr[0]), $enddateArr[2]+$datevar[2]);      
            }      
          }    
          
          
    // Create unix time stamp for the field value
          
    $requiredvalueArr explode("/"$_POST[$requiredfield]);
          
    $requiredvalue mktime(0,0,0,$requiredvalueArr[1], $requiredvalueArr[0], $requiredvalueArr[2]);
                
          
    // Now check that the date in the date field is between startdate and enddate
          
    if(($requiredvalue $startdate) || ($requiredvalue $enddate)) {
            
    $errormsg[$requiredfield] = "<span class=\"VAL_error\">This entry must be between " date("j/n/Y",$startdate) . " and " date("j/n/Y"$enddate) . ".</span>";
          }
        } 
    Just note that the start date must be from year 1902.

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