SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Date Variables?

  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    BC, Canada
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date Variables?

    Is there anyway in php to figure out the number of days between two dates? Given two dates by the user in the format YYYY-MM-DD can I find the number of days between the two. I can change the format if I have too.

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is one solution, if date format is kept consistently at YYYY-MM-DD.

    PHP Code:

    <?php

    $date0 
    "2002-11-25";   //date begin
    $date1 "2003-01-09";   //date end

    $dates0 explode("-",$date0);
    $dates1 explode("-",$date1);

    //Note: 86400 seconds in 1 day
    echo round((mktime(0,0,0,$dates1[1],$dates1[2],$dates1[0])-mktime(0,0,0,$dates0[1],$dates0[2],$dates0[0]))/86400)." days";

    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a couple of useful date functions I got from somewhere - however, both use the php date timestamp.

    PHP Code:
    function dateAdd ($interval,  $number$date) {
        
    $date_time_array  getdate($date);
          
        
    $hours =  $date_time_array["hours"];
        
    $minutes =  $date_time_array["minutes"];
        
    $seconds =    $date_time_array["seconds"];
        
    $month =    $date_time_array["mon"];
        
    $day =    $date_time_array["mday"];
        
    $year    =     $date_time_array["year"];

        switch (
    $interval) {
            case 
    "yyyy":
                
    $year    +=$number;
                break;                
            case 
    "q":
                
    $month +=($number*3);
                break;                
            case 
    "m":
                
    $month +=$number;
                break;                
            case 
    "y":
            case 
    "d":
            case 
    "w":
                
    $day+=$number;
                break;                
            case 
    "ww":
                
    $day+=($number*7);
                break;                
            case 
    "h":
                
    $hours+=$number;
                break;                
            case 
    "n":
                
    $minutes+=$number;
                break;                
            case 
    "s":
                
    $seconds+=$number;
                break;                
        }         
        
    $timestamp =    mktime($hours    ,$minutes$seconds,$month ,$day,    $year);
        return 
    $timestamp;
    }

    function 
    dateDiff ($interval$date1,$date2) {
      
    // get the number of seconds between the two dates
        
    $timedifference =  $date2 $date1;
        
      switch (
    $interval) {
        case 
    "w":
          
    $retval  $timedifference 604800;
          break;
        case 
    "d":
          
    $retval  $timedifference 86400;
          break;
        case 
    "h":
          
    $retval $timedifference 3600;
          break;        
        case 
    "n":
          
    $retval  $timedifference 60;
          break;        
        case 
    "s":
          
    $retval  $timedifference;
          break;        
      } 
    $retval = floor($retval);
    return $retval;
    }

    ?>


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
  •