How to calculate cumulative time in days (Count-up) in php?

Asked

Viewed 447 times

2

Guys, all right? I’m new to the forum.

I searched all over the place and couldn’t find a solution to what I need.

The concept is simple:

What I need to develop is basically a cumulative day counter that does not have a definite end date, so I set a start date (today for example, 18/07/18) and it shows in days/hours/minutes/seconds when time has passed since that date...

Ex: It passed 41days:09hours:03minutes:06seconds of the day 18/07/18

It is important that this date be fixed so that when reloading the page, the counter does not "reset" or start all over again and that anyone accessing the page see the same number/time.

The other posts I found trace the difference between two dates set, I want to define only the first date (start), there is a second date yet, he needs to accumulate time infinitely and give a echo/display onscreen

Countup

There is a website that does exactly what I need, but the problem is that they don’t release the counter code, they just let you embeddar a solution ready. follows link: https://www.tickcounter.com/countup

I would like to know how to do this manually so that I can customize 100% of its code (add HTML, CSS etc.) later and put on my website.

Thank you very much.

  • 1

    What have you tried?

  • Do you want to display this result just by loading the page or do you want it to update? Why is it so much more a matter of javascript than php

  • The following is an example with decimal places for days hours and minutes: https://answall.com/a/113086/3635

  • That’s exactly what I need: https://answall.com/questions/113082/timer-de-date/113086#113086 You nailed it! However I need it to update in real time on the screen (sorry I don’t know anything javascript).

1 answer

1


From what I understand, I think the code below does what you need.

$dtStart = new DateTime("2018-07-20"); 
$dtEnd   = new DateTime("2018-07-25 10:45:44"); 
$dtDiff = $dtStart->diff($dtEnd);
print $dtDiff->format("%d:%H:%I:%S");
  • Not really, in this code you show you are setting a final date in $dtEnd. I need the counter to accumulate time without a final forecast, you know?

  • Hello. Try $dtEnd = date('Y-m-d H:i:s'); this way you will get the current server date. If the PHP version was above 5.2.0, it could be like $dtEnd = new Datetime();

  • It worked out! Thank you.

Browser other questions tagged

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