calculate time difference in mintues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mijn naam

    #16
    Re: calculate time difference in mintues

    <student4lifer@ gmail.comschree f in bericht
    news:e3d752a2-7754-472f-8eac-75a1095d2085@v3 9g2000pro.googl egroups.com...
    Hello,
    >
    I have 2 time fields dynamically generated in format "m/d/y H:m".
    Could someone show me a good function to calculate the time interval
    difference in minutes? I played with strtotime() but but that only
    gave me difference in hours and only if the times were on the same day
    (after wrapping with date() function). TIA

    After this interesting discussion...

    Jessica's solution needs its input in seconds, so it requires you to call
    strtotime() yourself before calling that routine. It will then work,
    including those cases where DST is an odd number of times. It has the
    advantage that you can apply it also in cases where you want the difference
    in hours, months or even years.
    This said, Sheldon's solution does what you ask, and nothing more.

    Please be aware that "m/d/y H:m" is not very well formed. I for instance
    would read "07/06/05 12:00" as year 7, month 6, day 5, noon. Someone else
    (you) may read it as month 7, day 6, year 5. Still someone else may think it
    means day 7, month 6, year 5. And some poor soul may think it's midnight,
    which I would write as 00:00.

    $ php -f test.php ; unixtime 1183802400;unix time 1194606000
    timediff is set to 180060
    Unix timestamp 1: 1183802400
    Unix timestamp 2: 1194606000
    125 days, 1 hours, 0 minutes and 0 seconds
    which is 180060 minutes
    1st result of dateDiff: 0
    2nd result of dateDiff: 180060
    2007-07-07T10:00:00Z
    2007-11-09T11:00:00Z

    All solutions return 180060 which is, indeed, correct.

    bash function unixtime:

    unixtime ()
    {
    /bin/date -ud 1970-01-01\ 00:00\ +0000\ +${1}sec +%Y-%m-%dT%H:%M:%SZ
    }


    contents of test.php:

    <?php

    /* This code is NOT intended to show off any programming skills. I know much
    * of it can be left out, taken together, and so on.
    */

    $date1="07/07/07 12:00"; // warning: USA-centric mm/dd/yy , not ISO yy/mm/dd
    $date2="11/09/07 12:00";

    // modified sheldong example
    $timediff = floor((strtotim e($date2) - strtotime($date 1) + 30) / 60);

    echo "timediff is set to $timediff\n";


    // that example worked out a bit more
    $unixtime1=strt otime($date1);
    $unixtime2=strt otime($date2);

    echo "Unix timestamp 1: $unixtime1\n";
    echo "Unix timestamp 2: $unixtime2\n";

    $diff=abs($unix time1-$unixtime2);

    $days=floor($di ff/86400);
    $rest=$diff-$days*86400;
    $hours=floor($r est/3600);
    $rest=$rest-$hours*3600;
    $minutes=floor( $rest/60);
    $rest=$rest-$minutes*60;

    $totalminutes=$ days*1440+$hour s*60+$minutes;
    if ($rest>29) $totalminutes++ ;

    echo "$days days, $hours hours, $minutes minutes and $rest seconds\n";
    echo "which is $totalminutes minutes\n";

    function dateDiff($first Date, $secondDate, $interval = 'd')
    {
    $swapDate = $firstDate;
    $firstDate = min($swapDate, $secondDate);
    $secondDate = max($swapDate, $secondDate);
    if ($interval == 'm' || $interval == 'y')
    {
    $firstYear = date('Y', $firstDate);
    $secondYear = date('Y', $secondDate);
    $year = $secondYear - $firstYear;
    if ($firstYear != $secondYear){ $year--; }
    if ($interval == 'y'){ return $year; }
    $firstMonth = date('n', $firstDate);
    $secondMonth = date('n', $secondDate);
    $month = $firstYear == $secondYear ?
    abs($firstMonth - $secondMonth) :
    12 - $firstMonth + $secondMonth ;
    return $month % 12 + (12 * $year);
    }
    $intervals = array(
    'w' =(7 * 24 * 60 * 60) ,
    'd' =(24 * 60 * 60) ,
    'h' =(60 * 60) ,
    'n' =60 ,
    's' =1
    );
    return floor(abs($firs tDate - $secondDate) / $intervals[$interval]);
    }

    $result=dateDif f($date1,$date2 ,"n");
    echo "1st result of dateDiff: $result\n";

    // modified dateDiff example, because it expects time as its inputs, not
    "mm/dd/yy H:m"

    $result=dateDif f($unixtime1,$u nixtime2,"n");
    echo "2nd result of dateDiff: $result\n";


    ?>


    You could also specify a time zone to the dates. It even works for UTC:

    Modification in test.php:
    $date1="07/07/07 12:00 UTC"; // warning: USA-centric mm/dd/yy , not ISO
    yy/mm/dd
    $date2="11/09/07 12:00 UTC";

    run it:
    $ $ php -f test.php ; unixtime 1183809600;unix time 1194609600
    timediff is set to 180000
    Unix timestamp 1: 1183809600
    Unix timestamp 2: 1194609600
    125 days, 0 hours, 0 minutes and 0 seconds
    which is 180000 minutes
    1st result of dateDiff: 0
    2nd result of dateDiff: 180000
    2007-07-07T12:00:00Z
    2007-11-09T12:00:00Z


    As you can see: all solutions no longer have that extra hour.



    Comment

    Working...