A
A
ak_wi2018-04-26 02:06:28
PHP
ak_wi, 2018-04-26 02:06:28

How to do a time conversion to create a timer in PHP?

Task: when an event occurs, we need to write the current time + 3 hours into the Mysqli database (in what format is it better to do this so that we don’t have to worry about the conversion later?).
And then, when entering the page, you need to show a timer with hours, minutes and seconds left before the time recorded in the database. How is it easier in PHP to get the difference between the current time and the time in the future, and then decompose to calculate it in the format so many hours, minutes, seconds left.
Those. conditionally at midnight an event occurred, at three o'clock in the morning - the end of the Cinderella fairy tale. It's now two o'clock and eight minutes. it is necessary to output the time recorded in the database (three hours) minus the current one in a format suitable for a timer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bkosun, 2018-04-26
@bkosun

$d1 = new \DateTime("2018-04-26 03:00:00"); // Время события
$d2 = new \DateTime(); // Текущее время
        
$diff = $d1->diff($d2); // Разница

print_r($diff) ; // DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 1 [i] => 39 [s] => 21 [f] => -0.909875 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 1 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )

php.net/manual/ru/class.dateinterval.php
In the database, store in DateTime or UnixTimestamp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question