Raphael Jeger
Raphael Jeger

Reputation: 5232

Most efficient way to get seconds left in this hour

I need to get the remaining seconds in the actual hour for caching weather-data. What's the most efficient method of doing that?

Upvotes: 0

Views: 103

Answers (3)

Sameer Puri
Sameer Puri

Reputation: 1025

Simply use PHP's time function as follows:

<?php
function getSecondsRemaining()
{
    //Take the current time in seconds since the epoch, modulus by 3600 to get seconds in the hour, then subtract this from 3600 (# secs in an hour)
    return 3600 - time() % (60*60);
}
?>

This should get you the performance you want.

Upvotes: 4

CT14.IT
CT14.IT

Reputation: 1747

How about

$date = new DateTime("now");
$seconds_left = ( ( 59 - $date->format("i") ) * 60 ) + ( 60 - $date->format("s") ); 

Upvotes: 0

Sofiane Sadi
Sofiane Sadi

Reputation: 357

Here you go mate:

$currentMinute = date("i");
$minuteLeft = 60 - $currentMinute;
$secondLeft = $minuteLeft * 60;
$secondLeft += date("s");
echo $secondLeft;

Upvotes: 1

Related Questions