note 83745 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 03 Aug 2008 11:53:08 +0000 |
| Subject: | note 83745 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143108@lists.php.net to get a copy of this message | ||
Note Submitter: richard at happymango dot me dot uk
----
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;
}
}
?>