R
R
Roman Gor2014-10-22 18:16:31
PHP
Roman Gor, 2014-10-22 18:16:31

Does time belong to the current calendar day?

Task.
Find out if the specified time belongs to the current day, ignoring the elapsed time.
If it belongs, display the time. If not, display the time and date of the next calendar day.
Example 1:
The specified time is 20:21. The current time is 18:13. The specified time is greater than the current one - belongs.
Example 2: The specified time is 07:21. The current time is 18:13. The specified time is less than the current time - does not belong. Output the date of the next day.
My solution seemed too complicated for me. I decided to split the time values ​​with a bunch of conditions, and check each value with a bunch of conditions.
Interested in whether there is a better solution to this problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Melkij, 2014-10-22
@melkij

$rTargetTime = \datetime::createFromFormat('H:i', '23:45');
$bIsToday = $rTargetTime > new \datetime();

?

S
Sergey, 2014-10-22
Protko @Fesor

$time = '22:33';
$now = new \DateTime();
$time = new \DateTime(sprintf('%s %s:00', $now->format('Y-m-d'), $time));

$isTomorrow = $now->getTimestamp() > $time->getTimestamp();

O
OlegLazarenko, 2014-10-22
@OlegLazarenko

$time = strtotime('18:36');
$date = (time() > $time) ? date('d.m', $time + 86400) : date('d.m');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question