Remaining time system

Asked

Viewed 455 times

-2

I have a date generator and in it I mark the day that a certain event will happen. As seen in the image:

inserir a descrição da imagem aqui

This system returns a value in time(). I need a code that says the remaining time, for example, now is: 9/3/2016 - 19:26 and the event was scheduled for - 9/3/2016 - 21:00.

I want the code to return the remaining time. For example: 2 days, 1 hour and 10 minutes.

Here are some dates to help when doing:

23/03/2016 06:00 - time() - 1458723600

15/03/2016 09:00 - time() - 1458043200

I tried to adapt the code to one I already have, but it didn’t work.

I have this code that does the opposite of what I want, it shows the time that has passed.

function tempo($time){
        $diff = time() - $time;
        $seconds = $diff;
        $minutes = round($diff / 60);
        $hours = round($diff / 3600);
        $days = round($diff / 86400);
        $weeks = round($diff / 604800);
        $months = round($diff / 2419200);
        $years = round($diff / 29030400);
        if($seconds < 10) return "Agora mesmo";
        if($seconds < 60) return "$seconds segundos";
        else if($minutes < 60) return $minutes==1 ?'1 minuto':$minutes.' minutos';
        else if($hours < 24) return $hours==1 ?'1 hora':$hours.' horas';
        else if($days < 7) return $days==1 ?'1 dia':$days.' dias';
        else if($weeks < 4) return $weeks==1 ?'1 semana':$weeks.' semanas';
        else if($months < 12) return $months == 1 ?'1 mes':$months.' meses';
        else return $years == 1 ? '1 ano':$years.' anos';
    }

It returns values of type: 1 hour ago, 10 minutes ago, Right now, etc...

  • 2

    What have you tried? Do you want us to do the program for you? Show a little effort and show what you’ve tried to do, so people will have more empathy to help you. ;)

  • I tried to subtract real time with the time of the event and try to see the result but it did not work

  • I tried to adapt a system that counts the time that passed but it didn’t work look at the code that counts the time that passed: http://prntscr.com/adalix In this case it returns: 10 minutes ago, 1 hour ago etc...

  • 1

    Leandro click [Edit] and add the code to the question, and make it more objective, the way it is, it seems that you expect someone to do everything for you.

  • I edited I believe I made the base of the code a little clearer

  • 1

    You can improve, just try to focus on the following: what do I already have ready? What does my code need to do? How does it need to do?

  • 1

    There is a function called diff in the class DateTime, see an example: http://answall.com/a/113086/3635

Show 3 more comments

1 answer

2

Barter:

$diff = time() - $time;

For

$diff = $time - time();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.