Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created June 5, 2009 21:31
Show Gist options
  • Select an option

  • Save Oshuma/124514 to your computer and use it in GitHub Desktop.

Select an option

Save Oshuma/124514 to your computer and use it in GitHub Desktop.
<?php
// Rounds the given timestamp (string) up 15 minutes.
function round_timestamp($timestamp) {
$interval = 15; // minutes
$date_format = "%d-%b-%y %I:%M:%S %p %Z";
$time_array = strptime(strftime($date_format, strtotime($timestamp)), $date_format);
$minutes = $time_array['tm_min'];
$delta = $minutes % $interval;
$time_array['tm_min'] = $minutes + ($delta * ($delta < $interval / 2 ? -1 : 1));
$new_timestamp = mktime($time_array['tm_hour'],
$time_array['tm_min'],
$time_array['tm_sec'],
$time_array['tm_mon'],
$time_array['tm_mday'],
$time_array['tm_year']);
return strftime($date_format, $new_timestamp);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment