Calculating Days from 2Calender

Discussion in 'PHP' started by ausgezeichnete, Dec 9, 2007.

  1. #1
    Am using 2 calenders in my form:
    
    $s=split("[/]",$date);
    $e=split("[/]",$datem);
    for($i=0;$i<=count($s);$i++)
    {
      $year1=$s[0];
      $mon1=$s[1];
      $day1=$s[2];
    }
    for($i=0;$i<=count($e);$i++)
    {
      $year2=$e[0];
      $mon2=$e[1];
      $day2=$e[2];
    }
    
    
    PHP:
    and i want to calculate how many days he choosed??
    i did so
    echo $day=$day2-$day1;
    PHP:
    but the problem,if he choose for example
    From:2007/12/30
    to:2008/12/09
    i will have -21days
    can anyone help me???
     
    ausgezeichnete, Dec 9, 2007 IP
  2. ausgezeichnete

    ausgezeichnete Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    and if i choosed
    From:2007/12/30
    To:2008/01/02
    it gave me 28
    and it suppose to be 3 days!!!!!!!!
     
    ausgezeichnete, Dec 9, 2007 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    You need to convert to timestamp for these things. I ain't PHP guru, but hope this helps :)

    
    $s = strtotime($date); // converts start days to timestamp
    $e = strtotime($datem); // converts end days to timestamp
    
    $tDiff = $e - $s; // calculate the difference
    $days = ceil($tDiff / 86400); // There are 86400 seconds in a day. ceil() rounds the fractions up, can't have decimal days
    echo $days; // outputs the number of days
    
    PHP:
     
    Kaizoku, Dec 9, 2007 IP
  4. ausgezeichnete

    ausgezeichnete Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thnxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx it worked
     
    ausgezeichnete, Dec 9, 2007 IP