Timestap

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

    Timestap

    I have the following problem

    When I subtract 2 (timestamp) dates, it reports back wrong time difference.


    Can it be done like this at all?

    $datee="2005-02-01 19:54:00"
    $datee1="2005-02-01 18:58:00"

    $date1=strtotim e($object["datee"]);
    $date2= strtotime($obje ct["datee1"]);

    $diff=0;

    $diff = $date1-$date2;


    $days = round($diff/86400, 0);
    $hours = round(($diff%86 400)/3600, 0);
    $minutes = round(($diff%36 00)/60, 0);
    $seconds = $diff%60;

    Tnx











  • Andy Hassall

    #2
    Re: Timestap

    On Wed, 2 Feb 2005 21:59:42 +0100, "pero" <pero@net.hr> wrote:
    [color=blue]
    >I have the following problem
    >
    >When I subtract 2 (timestamp) dates, it reports back wrong time difference.
    >
    >
    > Can it be done like this at all?
    >
    >$datee="2005-02-01 19:54:00"
    >$datee1="200 5-02-01 18:58:00"
    >
    >$date1=strtoti me($object["datee"]);
    >$date2= strtotime($obje ct["datee1"]);[/color]

    Please post _runnable_ code - there's two parse errors and two undefined uses
    of $object. Making minimal changes to your code so it runs and actually
    displays something gives:

    <?php
    $datee="2005-02-01 19:54:00";
    $datee1="2005-02-01 18:58:00";

    $date1=strtotim e($datee);
    $date2= strtotime($date e1);

    $diff=0;

    $diff = $date1-$date2;

    $days = round($diff/86400, 0);
    $hours = round(($diff%86 400)/3600, 0);
    $minutes = round(($diff%36 00)/60, 0);
    $seconds = $diff%60;

    print "days: $days<br>";
    print "hours: $hours<br>";
    print "minutes: $minutes<br>";
    print "seconds: $seconds<br>";
    ?>

    Output is:

    days: 0
    hours: 1
    minutes: 56
    seconds: 0

    Minutes are fine, hours are not. It's because you've used round. You want
    floor; 56 minutes is 0.933 hours, so you want to round down, not up.

    $days = floor($diff/86400);
    $hours = floor(($diff%86 400)/3600);
    $minutes = floor(($diff%36 00)/60);
    $seconds = $diff%60;

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    Working...