开发者

Unexpected results when subtracting two time values [closed]

开发者 https://www.devze.com 2023-03-24 15:41 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.

Closed 8 years ago.

Improve this question

This is really strange. I have two time values.

First time:

$t1 = 1311165885;

Second time (which is bigger than the first time):

$t2 = 1312049530;

I am going to subtract the times like this:

$diff = $t2-$t1;

When I do an echo $diff this is coming out:

echo 'Time_diff: ' . $diff; 
Time_diff: -1311163874

The actual result should be 883645. What is wrong?

Thanks in advance!

Craphunter

Then here is my code:

<?php 
$current_time = DataConverter::makeDatetime();

foreach ($order as $list) {
    echo '-----------'.'</br>';
    $create_unix_time = DataConverter::makeTimestamp($list->getCreate());
    $diff = $current_time-$create_unix_time;

    if ($diff >= 864000) {
        echo开发者_开发问答 'aus db: ' .  $list->getErstellt() . '</br>';
        echo $list->getId() . '</br>';
        echo 'old';
    }
    else {
    }
}

?>


<?php

$t1 = 1311165885;
$t2 = 1312049530;
$diff = $t2-$t1;

echo 'Time 1: ' . date('Y-m-d H:i:s', $t1) . PHP_EOL;
echo 'Tim2 2: ' . date('Y-m-d H:i:s', $t2) . PHP_EOL;
echo 'Time diff: ' . $diff . PHP_EOL;

... prints this:

Time 1: 2011-07-20 14:44:45
Tim2 2: 2011-07-30 20:12:10
Time diff: 883645

What you say you are getting does look like a regular timestamp rather than a diff:

echo date('Y-m-d H:i:s', 1311163874) . PHP_EOL;

... prints:

2011-07-20 14:11:14

There's probably a mistyped variable name somewhere in your code.


I ignore the data type result of method DataConverter::makeTimestamp. Maybe that is a wrong assumption suppose the result is integer. Maybe. But, you can force the data type to integer and test if that is the problem.

$current_time = intVal( DataConverter::makeDatetime() );

and

$create_unix_time = intVal( DataConverter::makeTimestamp( $list->getCreate() ) );
0

精彩评论

暂无评论...
验证码 换一张
取 消