Jump to content

Displaying Minutes and Seconds until next half hour


McBryver

Recommended Posts

Unfortunately I do not know how to go about doing this in php any simple examples of time and date will be great.

 

 

So what I am trying to do is, display to minutes until the next half hour as well as the seconds until the next minute.  Eg.

<?php echo $minutes; ?>:<?php echo $seconds; ?>

 

 

Any help using the time and or date functions in this case will be great.

 

Thank you for your stupendous help.

Brian

PHP dates and times can be tricky; Just remember that all times in PHP are large numbers that represent seconds. When more time has passed, the number obviously becomes larger.

 

You can get the current time, in seconds, with time()

You can change that time into a string, like "November 3, 2010, 10:21 AM", with date()

You can change that string into seconds again with strtotime()

 

Here's an example; I tried to make it as simple as possible.

 

// Get the current time
$now = time();

// Get information about the current time
$currentHour = date('H', $now); // "H" returns the current hour
$currentMinute = date('i', $now); // "s" returns the current minute

// Determine when the next half-hour is
if ($currentMinute < 30) {
  $nextHalfHour = date($currentHour . ':30'); // String representation
  $nextHalfHour = strtotime($nextHalfHour); // Turn into an integer timestamp
} else {
  $nextHalfHour = date(($currentHour + 1) . ':00'); // String representation
  $nextHalfHour = strtotime($nextHalfHour); // Turn into an integer timestamp
}

// Find the time between the current time and the next half hour
$difference = $nextHalfHour - $now;

// Echo the time left
echo 'There are ';
echo date('G', $difference) . ' hours and ';
echo date('i', $difference) . ' minutes until the next half-hour.';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.