Date Timer

Asked

Viewed 791 times

0

Good is the following I have a certain date and a certain time.

I wanted php to show me how many days, hours, minutes and seconds are left for that particular date and that particular time.

I mean, I have a date and a time in a variable and I want an echo in php to show me how long it takes to get to that date.

It doesn’t have to be real time!

I want the end date to be hour and minute and second.

Just enter the site and appear how much time is left, no need to decrease or anything!

I hope that was understood. Thank you.

  • Changing in real time? With php I don’t think it’s possible, maybe with javascript.

  • It doesn’t have to be in real time, just go on the site and tell me just how much time is left for that date/hour/minute/second.

  • Oh yes, can you add that to the question? just to be clear when someone is going to answer.

1 answer

2


Using the example of this reply, but with hours minutes and seconds:

$termina = new \DateTime('2017-12-11 11:14:15');
$hoje    = new \DateTime();

$intervalo = $hoje->diff($termina);

echo "Intervalo é de {$intervalo->y} anos, {$intervalo->m} meses e {$intervalo->d} dias, {$intervalo->h} horas, {$intervalo->i} minutos, {$intervalo->s} segundos";

If you want to add milesegundos do so:

2017-12-11 11:14:15.638276

The 638276 is the value of milesegundo and cannot be separated from the .

Documentation:


To add decimals you can use str_pad or sprintf, as in this answer: /a/51828/3635 (with the example of sprintf nominated by fellow @Wallacemaxters), so should stay:

$termina = new \DateTime('2050-12-11 11:14:15');
$hoje    = new \DateTime();

$intervalo = $hoje->diff($termina);

printf('%04d/%02d/%02d %02d:%02d:%02d', $intervalo->y, $intervalo->m, $intervalo->d, $intervalo->h, $intervalo->i, $intervalo->s);

An online example: https://ideone.com/DkK2WS

  • But for example I want $ends I have a specific hour with minutes and seconds. How can I do this?

  • @Gonçalo

  • Okay, thank you.

  • @Gonçalo edited. Just read the documentation I passed, but ok it’s good to always have a practical example too ;)

  • By the way there is some parameter that indicates the second milestones also?

  • @Gonçalo yes, it’s in the documentation... Eita preguiça hein... (joke :) I will edit

  • And in the interval, ms means milliseconds or in the interval there is no?

  • @Gonçalo do you want to read to humans or to a computer? milesegundos is usually not human reading but for computer accuracy. If you want precision then don’t use the class, use primitive values like int, float, etc.

  • I wanted to do it by this method really, it’s for humans, but tell me something doesn’t have any 2-house parameter next to the second? If you don’t have it, that’s fine.

  • @Gonçalo explains better, he’s confused, than "houses"?

  • I mean if there is any parameter, which shows 2 decimal places next to the second.

  • @Gonçalo similar to this $intervalo->s ? But for a million seconds?

  • That’s right, that’s right!

  • @Gonçalo looked at the documentation and it seems that it does not have, as I said the milesegundos/ microsegundos are used for precision and hardly interesting for humans and for this reason it seems that it does not exist. But if you find something later I’ll edit.

  • All right, thanks anyway!

  • @Gonçalo I know it’s been a long time and surely you already solved this, but just to let you know at the time I didn’t even notice the str_pad and the sprintf or printf, so I added this the answer, so you can have the decimals in the hours and days ;)

Show 11 more comments

Browser other questions tagged

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