SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Compare two date using mktime help?

    Hi guys
    I have a string that is submitted via a form and need to check to see if it is within a date range (10/04/09 - 05/06/09). String format is d/m/y. Ive been playing around with the code below but have had no luck. Im not new to php but Ive had next to no experience with date functions. I have also tried using strtotime() but it changes the d/m order around. If any body could help me out it would be much appreciated

    $date1 = date("d/m/y", mktime(0, 0, 0, 04, 10, 2009));
    $date2 = date("d/m/y", mktime(0, 0, 0, 06, 05, 2009));
    $date3 = $_POST['date'];
    $parts = explode('/', $date3);
    $date4 = date("d/m/y", mktime(0 ,0, 0, $parts[1], $parts[0],$parts[2]));

    echo $date1."<br />".$date2."<br />".$date3."<br />".$date4."<br />";

    if (($date4 < $date1) || ($date4 > $date2)){
    echo 'wrong date';
    }
    else{echo 'success';};

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    May be easier for you if you think of it not in terms of a formatted date, but as a timestamp:
    PHP Code:
    <?php
    $MaxDate 
    mktime(00004102009);
     
    $MinDate mktime(00006052009);
     
    $GivenDateString $_POST['date'];
    $GivenDateArray explode('/'$GivenDateString);
    $GivenDate mktime(000$GivenDateArray[1], $GivenDateArray[0], $GivenDateArray[2]);
    if(
    $GivenDate $MaxDate){
         
    // Date is too large
     
    }else if($GivenDate $MinDate){
         
    // Date is too small
     
    }else{
        
    // Date is valid
    }
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Thanks for posting so quickly
    This works great - you have been a huge help!


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
  •