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 3 of 3
  1. #1
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Calculating the difference between two dates.

    I have two user imputted dates in the format (YYYY-MM-DD) and i need to work out the number of days between the two dates. I found this code but i cant seem to get it to work! Does anone have any alternaivs or suggestions?

    PHP Code:
        function dateDiff($dformat$endDate$beginDate)
    {
    $date_parts1=explode($dformat$beginDate);
    $date_parts2=explode($dformat$endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return 
    $end_date $start_date;

    This is my version of the code:
    PHP Code:
        Function staylength(){
        
        
    $checkin=$_REQUEST['checkin'];
        
    $checkout=$_REQUEST['checkout'];
        
                
    $date_parts1=explode("-"$checkin);
                
    $date_parts2=explode("-"$checkout);
                
    $checkin=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
                
    $checkout=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);

             
    $staylength $checkout $checkin;
            
            echo 
    $staylength;


  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    So many different ways to do this ... just one example below.
    I didn't try your code, but I think you need to bring in the variables
    with the function, so they can be used "inside" the function.

    PHP Code:
    $days staylength($_REQUEST['checkin'],$_REQUEST['checkout']);

    Function 
    staylength($checkin,$checkout){ 
              
    $date_parts1=explode("-"$checkin); 
                
    $date_parts2=explode("-"$checkout); 
                
    $checkin=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]); 
                
    $checkout=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]); 

             
    $staylength $checkout $checkin
             
            echo 
    $staylength


    PHP Code:
    <?php

    // $days is the number of days between the dates

    $init_day "5";
    $init_mth "6";
    $init_yr "2008";

    $dst_day "15";
    $dst_mth "6";
    $dst_yr "2008";

    //first convert to unix timestamp
    $init_date mktime(0,0,0,$init_mth,$init_day,$init_yr);
    $dst_date mktime(0,0,0,$dst_mth,$dst_day,$dst_yr);

    $offset $dst_date-$init_date//Depending on which offset type you want, switch order

    $days floor($offset/60/60/24);

    echo 
    $days;
    ?>

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    srttotime combined with strftime. Three lines of code
    PHP has the greatest time handing of any language of used.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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