subtract current time to end of day - php

How do I subtract the current time till the end of the day in PHP?

2:33:27 - 12:00:00 (midnight)

I want a result of 9 hours 27 minutes and 33 seconds
LVL 1
bschwartingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
This will do it as spec'd

<?php

$now = date("Y-m-d");
$tomorrow = strtotime("$now +1 day");
$timeLeft = $tomorrow - time();

echo date("H \h\o\u\\r\s i \m\i\\n\u\\t\e\s s \s\e\c\o\\n\d\s",$timeLeft);
?>

The escaping is to stop the date function interpreting the text as directives.
Beverley PortlockCommented:
If you want to split out the hours etc then alter the above like so

<?php

$now = date("Y-m-d");
$tomorrow = strtotime("$now +1 day");
$timeLeft = $tomorrow - time();

$hours = date("H",$timeLeft);
$mins  = date("i",$timeLeft);
$secs  = date("s",$timeLeft);

echo "Time left is $hours hours, $mins minutes and $secs seconds";
?>
bschwartingAuthor Commented:
the 1st option gives me 4 hours instead of 9.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Beverley PortlockCommented:
Well, it would do - there is only 4 hours (or so) to midnight - in this timezone.....
Roger BaklundCommented:
Try this:
$one_day = 60*60*24;
$rest_of_day = $one_day - (time() + date('Z')) % $one_day;
echo gmdate('G \h\o\u\r\s i \m\i\n\u\t\e\s \a\n\d s \s\e\c\o\n\d\s',$rest_of_day);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bschwartingAuthor Commented:
perfect, thanks!
Beverley PortlockCommented:
OK here it is modified to use your example time (which gives 9 hours to go)

<?php
$sampleTime = strtotime("14:33:27");

$now = date("Y-m-d");
$tomorrow = strtotime("$now +1 day");
$timeLeft = $tomorrow - $sampleTime;

$hours = date("H",$timeLeft);
$mins  = date("i",$timeLeft);
$secs  = date("s",$timeLeft);

echo "Time left is $hours hours, $mins minutes and $secs seconds";
?>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.