www.webdeveloper.com
Results 1 to 4 of 4

Thread: Want to convert DateTime difference result to Minutes

  1. #1
    Join Date
    Dec 2008
    Posts
    10

    Want to convert DateTime difference result to Minutes

    Hi Friends,

    I'm struck up in converting the DateTime difference result as Minutes, could you please anyone help me on this?

    Here am explaining my trouble:
    I'm fetching start and end time from MySQL (saved in the form of datetime type; e.g $start = '2010-10-22 11:15:09'; $end = '2010-10-23 19:08:16'
    using the below function I got the difference perfectly!
    like : Array ( [days] => 1 [hours] => 7 [minutes] => 53 [seconds] => 7 )

    I want to have the complete datetime difference in the form of minutes?
    <?php
    function get_time_difference( $start, $end ){
    $uts['start'] = strtotime( $start );
    $uts['end'] = strtotime( $end );
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
    if( $uts['end'] >= $uts['start'] )
    {
    $diff = $uts['end'] - $uts['start'];
    if( $days=intval((floor($diff/86400))) )
    $diff = $diff &#37; 86400;
    if( $hours=intval((floor($diff/3600))) )
    $diff = $diff % 3600;
    if( $minutes=intval((floor($diff/60))) )
    $diff = $diff % 60;
    $diff = intval( $diff );
    return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
    }
    else
    {
    trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );
    }
    }
    else
    {
    trigger_error( "Invalid date/time data detected", E_USER_WARNING );
    }
    return( false );
    }


    $start = '2010-10-22 11:15:09';
    $end = '2010-10-23 19:08:16';

    $diff = get_time_difference($start,$end);
    print_r ($diff);
    // echo http_build_query($diff);
    ?>
    Regards,
    Thirilog
    Last edited by Thirilog; 10-25-2010 at 07:54 AM.

  2. #2
    Join Date
    Sep 2010
    Posts
    160
    I suggest you check out the help for the date_diff() function and go from there.

    I think you already have a method that does that in the PHP language, no need to create it yourself.

    From the PHP manual on DateTime object:

    Code:
    <?php
    $datetime1 = date_create('2009-10-11');
    $datetime2 = date_create('2009-10-13');
    $interval = date_diff($datetime1, $datetime2);
    echo $interval->format('&#37;R%d days');
    ?>
    You should only have to change the "%R%d" to whatever you want it to display (example minutes).

    Archie
    Last edited by zimonyi; 10-25-2010 at 08:38 AM.

  3. #3
    Join Date
    Dec 2008
    Posts
    10
    Many Thanks!

    To be the temp solution i have followed the below steps, however i'll try
    $day = $diff['days'];
    $dayc = (24*$day)*60;
    $hrs = $diff['hours'];
    $hrsc = $hrs*60;
    $mins = $diff['minutes'];
    $sec = $diff['seconds'];
    $secc = $sec/60;
    $Tmins = $dayc+$hrsc+$mins+$secc;
    Regards,
    Thirilog

  4. #4
    Join Date
    Apr 2013
    Posts
    1

    easiest way

    PHP Code:
    $difference $datetime1->diff($datetime2);
    echo 
    $difference->format("%i%a minutes"); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles