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

Link to comment
Share on other sites

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.';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.