note 58047 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 15 Jul 2007 12:45:04 +0000 |
| Subject: | note 58047 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-128569@lists.php.net to get a copy of this message | ||
Note Submitter: MonstersAndCritics.com
----
Hi,
a simple function which returns the desired interval around the given timestamp as an array with the
elements 'to' and 'from'. Possible intervals are: "minute",
"hour", "day", "week", "month", "year". If the
requested interval is "week", you can additionally provide the first_day_of_week.
<?php
function time_to_interval($timestamp, $interval, $first_day_of_week="Sunday")
{
$interval_array = array();
$interval_array['from'] = $timestamp;
$interval_array['to'] = $timestamp;
switch ($interval)
{
case "minute" :
$from = date("Y-m-d H:i:00", $timestamp);
$to = date("Y-m-d H:i:00", strtotime($from . " + 1 minute"));
break;
case "hour" :
$from = date("Y-m-d H:00:00", $timestamp);
$to = date("Y-m-d H:00:00", strtotime($from . " + 1 hour"));
break;
case "day" :
$from = date("Y-m-d 00:00:00", $timestamp);
$to = date("Y-m-d 00:00:00", strtotime($from . " + 1 day"));
break;
case "week" :
$to = date("Y-m-d 00:00:00", strtotime("this
$first_day_of_week", $timestamp));
$from = date("Y-m-d 00:00:00", strtotime("last
$first_day_of_week", $timestamp));
break;
case "month" :
$from = date("Y-m-01 00:00:00",$timestamp);
$to = date("Y-m-01 00:00:00", strtotime($from . " + 1 month"));
break;
case "year" :
$from = date("Y-01-01 00:00:00", $timestamp);
$to = date("Y-01-01 00:00:00", strtotime($from . " + 1 year"));
break;
}
$interval_array['from'] = strtotime($from);
$interval_array['to'] = strtotime($to);
return $interval_array;
}
# Use it like :
$timestamp = time();
$interval = time_to_interval($timestamp, "hour");
if ($mytime >= $interval['from'] && $mytime < $interval['to']){
// do something
}
?>