SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue in getting the time difference between two variables

    I have two textboxes that inputted time, and I want to get the difference between two time.

    for example:

    $IN = 13:35;
    $OUT = 17:35;

    $OTHours = ($OUT - $IN);

    $OTHours = 4.00;


    and it is correct, but I have a scenario like this:

    $IN = 21:35;
    $OUT = 05:35;

    $OTHours = -16.00;

    it should be 8.00.

    Any help is highly appreciated.

    Thank you..

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:

    $IN          
    '21:35';
    $OUT       '05::35';
    $OUT       = ($IN $OUT) ? ('24:00' '05:35') : $OUT;
    $OThours = ($OUT $IN); 
    Above script works in PHP. What programming language are you using?

    I have never used $OThours, new to me - learn something everyday


    //
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When i tried this the 05:35 becoome 29..


    What if i add date in time?it should be much better?


    Than you

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Until others give another solid way, try to understand this:
    PHP Code:
    $in '21:35';
    $out '05:35';
    // check if the out time is less than the in time
    if(strtotime($out) < strtotime($in)){
        
    $in date('Y-m-d') . ' 21:35'// append the in time with today's date
        
    $out date('Y-m-d'strtotime("tomorrow")) . ' 05:35'// append date out time with next day/tomorrow
        
    $diff strtotime($out) - strtotime($in);
    }
    else{
        
    $diff strtotime($out) - strtotime($in);
    }
    echo 
    round(($diff 60) / 60); 
    Is it something you are looking for?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This is tricky

    Best way is probably strtotime as mentioned before. However you must take into consideration that an $in time of 23:40 and an $out time of 02:30 the $out time is on the next day so...

    PHP Code:
    $in '23:40';
    $out '02:30';

    $inTimestamp strtotime($in);
    $outTimestamp strtotime($out);
    if((
    substr($out01) == 0) && substr($in01) != 0){
        
    $outTimestamp strtotime("tomorrow"$outTimestamp);
    }

    $diff $outTimestamp $inTimestamp;
    echo 
    round($diff 360); 
    Something like this should work...

  6. #6
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this new code:
    Code:
    $from_time  = strtotime($IN);
    $to_time = strtotime($OUT);
    $OTHours = round(abs($to_time - $from_time) / 3600);
    $IN = 2011-12-22 13:30;
    $OUT = 2011-12-22 15:30;

    from this code the $OTHours result is 1.00
    it should be 1.30

    by the way the OTHours field is Decimal Type.

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    round() makes a round number so thats why you get 1.00 instead of 1.30. If the true value was 1.60 you would get 2.00 you have to provide a "precision" to fix this:

    PHP Code:
    $OTHours round(abs($to_time $from_time) / 36002); 

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,883
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:
    function index()


    $in       '19:30'
    $secs_in  strtotime$in );
    $times    = array
    (
      
    '19:30'
      
    '19:31'
      
    '23:35'
      
    '01:35'
      
    '03:35'
      
    '05:35'
      
    '07:35'
      
    '19:29'
    );
    echo 
    "
      <!doctype html>
        <head>
          <title>Time difference</title>
          <style type='text/css'>
            body  {text-align:center; font-size:14px; line-height:1.8em}
            table {background-color:#cfc; margin:2em auto}
            th {width:10em}
          </style>  
        </head>
      <body>  
    "
    ;
    echo 
    "<table>
            <tr style='font-weight:700; color:#f00'>
              <th> time in    </th>
              <th> time out   </th>
              <th> secs_in    </th>
              <th> secs_out   </th>
              <th> secs_diff  </th>
              <th> mins_diff  </th>
              <th> hour:min   </th>
            </tr>
          "
    ;

    foreach ( 
    $times as $out ):
      
    # maybe $out time is less than $in time
      
    $secs_out  strtotime$out );   //echo '<br />$out ---> ' .$out .' == ' .$secs_out  .' seconds';  
      
    if ( $secs_out $secs_in )
      {
        
    $secs_out 24 60 60 $secs_out// add full day of seconds to time in
      
    }
      
      
    $secs_diff  $secs_out $secs_in;
      
    $mins_diff  sprintf('%d',        $secs_diff 60);
      
    $duration   sprintf('%02d:%02d'$mins_diff/60$mins_diff 60);
      
      echo 
    '<tr>';
        echo 
    '<td>' .$in                       .'</td>';
        echo 
    '<td>' .$out                      .'</td>';
        echo 
    '<td>' .number_format($secs_in)   .'</td>';
        echo 
    '<td>' .number_format($secs_out)  .'</td>';
        echo 
    '<td>' .number_format($secs_diff) .'</td>';
        echo 
    '<td>' .$mins_diff                .'</td>';
        echo 
    '<td>' .$duration                 .'</td>';
      echo 
    '</tr>';
    endforeach;
    echo 
    '</table><body></html>';
    die; 

    Output:
    PHP Code:
    time in     time out     secs_in     secs_out     secs_diff     mins_diff     hour:min
    19
    :30    19:30    1,328,553,000    1,328,553,000    0    0    00:00
    19
    :30    19:31    1,328,553,000    1,328,553,060    60    1    00:01
    19
    :30    23:35    1,328,553,000    1,328,567,700    14,700    245    04:05
    19
    :30    01:35    1,328,553,000    1,328,574,900    21,900    365    06:05
    19
    :30    03:35    1,328,553,000    1,328,582,100    29,100    485    08:05
    19
    :30    05:35    1,328,553,000    1,328,589,300    36,300    605    10:05
    19
    :30    07:35    1,328,553,000    1,328,596,500    43,500    725    12:05
    19
    :30    19:29    1,328,553,000    1,328,639,340    86,340    1439    23:59 
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014


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
  •