note 83746 added to function.round
| From: | richard at happymango dot me dot uk | Date: | Tue, 10 Jun 2008 11:13:59 +0000 |
| Subject: | note 83746 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140297@lists.php.net to get a copy of this message | ||
In complement to the my last post, roundTime(), here are two other functions. ceilTime() and
floorTime() which round up or down to the specified increment respectively.
<?php
//RSS: 10/06/08 rounds a timestamp to the next specified increment
//only works for increments less than or equal to 1 hour
//EG: ceilTime("15 Minutes"); rounds the time right now to the next 15 minutes 11:12
rounds to 11:15
function ceilTime($increment, $timestamp=0)
{
if(!$timestamp) $timestamp = time();
$increment = strtotime($increment, 1) - 1;
$this_hour = strtotime(date("Y-m-d H:", strtotime("-1 Hour",
$timestamp))."00:00");
$next_hour = strtotime(date("Y-m-d H:", strtotime("+1 Hour",
$timestamp))."00:00");
$increments = array();
$differences = array();
for($i = $this_hour; $i <= $next_hour; $i += $increment)
{
if($i > $timestamp) return $i;
}
}
//RSS: 10/06/08 rounds a timestamp to the last specified increment
//only works for increments less than or equal to 1 hour
//EG: floorTime("15 Minutes"); rounds the time right now to the last 15 minutes 11:12
rounds to 11:00
function floorTime($increment, $timestamp=0)
{
if(!$timestamp) $timestamp = time();
$increment = strtotime($increment, 1) - 1;
$this_hour = strtotime(date("Y-m-d H:", strtotime("-1 Hour",
$timestamp))."00:00");
$next_hour = strtotime(date("Y-m-d H:", strtotime("+1 Hour",
$timestamp))."00:00");
$increments = array();
$differences = array();
for($i = $next_hour; $i >= $this_hour; $i -= $increment)
{
if($i < $timestamp) return $i;
}
}
?>
----
Server IP: 92.48.74.199
Probable Submitter: 80.194.36.8
----
Manual Page -- http://www.php.net/manual/en/function.round.php
Edit -- https://master.php.net/note/edit/83746
Del: integrated -- https://master.php.net/note/delete/83746/integrated
Del: useless -- https://master.php.net/note/delete/83746/useless
Del: bad code -- https://master.php.net/note/delete/83746/bad+code
Del: spam -- https://master.php.net/note/delete/83746/spam
Del: non-english -- https://master.php.net/note/delete/83746/non-english
Del: in docs -- https://master.php.net/note/delete/83746/in+docs
Del: other reasons-- https://master.php.net/note/delete/83746
Reject -- https://master.php.net/note/reject/83746
Search -- https://master.php.net/manage/user-notes.php