Date Timer [duplicate]

0

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

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

That is, I have a date and a time in a variable and I want an echo in php to show me how much time is left to reach that date.

No need to be real-time!

I want the end date to have a minute and a second time.

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

I hope it has been understood. Thank you.

    
asked by anonymous 13.02.2016 / 20:08

1 answer

2

Using the example of this response , but with hours and minutes 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 milliseconds do this:

2017-12-11 11:14:15.638276

The 638276 is the millisecond value and can not be separated from .

Documentation:

  • link
  • link

  • $intervalo->y returns year

  • $intervalo->m month returns
  • $intervalo->d returns day
  • $intervalo->h returns time
  • $intervalo->i returns minutes
  • $intervalo->s returns seconds

To add decimal places you can use str_pad or sprintf , as in this answer: link ( with the example of sprintf indicated by the colleague @WallaceMaxters), then it should be:

$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 example online: link

    
13.02.2016 / 20:39