Skip to content

Instantly share code, notes, and snippets.

@wzulfikar
Last active September 20, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wzulfikar/acbb16cc2c8aaa4a03e9394078a96688 to your computer and use it in GitHub Desktop.
Save wzulfikar/acbb16cc2c8aaa4a03e9394078a96688 to your computer and use it in GitHub Desktop.
Get seconds difference from current time to given time. Will always compare closest time.
<?php
function diffTo($toTime)
{
$now = strtotime('now');
$next = strtotime('today ' . $toTime);
if($next <= $now){
$next = strtotime('tomorrow ' . $toTime);
}
return $next - $now;
}
// example
$nextTime = '13.19';
echo 'current time is ' . date('Y-m-d H:i:s');
echo '<br/>';
echo 'next time is ' . $nextTime;
echo '<br/>';
echo 'diff to next time in minutes: ' . round(diffTo($nextTime) / 60, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment