McBryver Posted November 2, 2010 Share Posted November 2, 2010 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 Quote Link to comment Share on other sites More sharing options...
Airzooka Posted November 3, 2010 Share Posted November 3, 2010 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.'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.