473,328 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,328 software developers and data experts.

Normalizing a timestamp to an interval

HaLo2FrEeEk
404 256MB
I have the need to take the current timestamp and normalize it to an interval. I had a working example of this for 10 minute intervals, but for some reason it won't work with days.

So for example, I want to take the timestamp right now and get the timestamp of the most recent occurance of 3am was. It might be march 24th at 2am, which means that 3am hasn't hit yet so I need march 23rd's 3 am timestamp.

I tried using strtotime('3am') but it gets today's 3 am, even if that time hasn't happened yet. How can I force it to get the most recent 3 am, not just today's?

Does this make any sense at all? I not, imagine this, I've got a timestamp, 1300955926, which represents the time 3/24/11 at 2:38:46 am CST. I need the timestamp for the most recent 10 minute interval. We'll call our timestamp $now, for ease. To get the 10 minute interval, I'd do this simple math:

$now - ($now % 600);

600 being the number of seconds in a 10 minute period, I subtract the remainder of $now / 600 from $now and I get a timestamp that represents the new time, 3/24/11 at 2:30:00 am CST. For some reason though I can't get this to work for days with the same math, but mod'ing by 86400 (seconds in a day) instead.
Mar 24 '11 #1
1 2756
dgreenhouse
250 Expert 100+
Maybe this?

Since it's 04:27 on March 24th my time I used 5.

The call to the function below returned:

Wed, 23 Mar 11 05:27:50 -0700

With a little work (I assume) the function could work for any number of intervals.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. echo '<pre>';
  4.  
  5. echo date( DATE_RFC822, getMostRecentTimeOfDay(5) );
  6.  
  7. echo '</pre>';
  8.  
  9. /**
  10.  * Return most recent timestamp for a given hour
  11.  *
  12.  * @param integer $hour_test 
  13.  * @return integer $timestamp
  14.  */
  15. function getMostRecentTimeOfDay($hour_test=0) {
  16.  
  17.   // Ensure $hour_test is correct
  18.   $hour_test = 
  19.       is_integer($hour_test) && $hour_test >= 0 && $hour_test <=23 
  20.         ? $hour_test 
  21.         : 0;
  22.  
  23.   $today = localtime(time(),true);
  24.  
  25.     if ($today['tm_hour'] < $hour_test) {
  26.       // Get yesterday's date/time/hour
  27.       // (seconds may be off by 1 here)
  28.         $today = localtime(strtotime("-1 day"),true);
  29.     }
  30.  
  31.     // Get timestamp
  32.     $timestamp = 
  33.       mktime(
  34.           $hour_test,
  35.             $today['tm_min'],
  36.             $today['tm_sec'],
  37.             $today['tm_mon'] + 1,
  38.             $today['tm_mday'],
  39.             $today['tm_year'] + 1900
  40.         );
  41.  
  42.   return $timestamp;
  43. }
  44. ?>
  45.  
Mar 24 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vilson farias | last post by:
Greetings, I was doing some tests with timestamps and intervals and I've discovered, for my surprise, that I can't set precision for resulting fields of type interval when I'm executing a query....
1
by: YONETANI Tomokazu | last post by:
Hello. After upgrading a test database to V8.2 with FixPak7a, too see if the production servers can be safely upgraded, I noticed that inserts on an insertable view which consists of multiple...
1
by: Liviu BURCUSEL | last post by:
Hello ! It is late night and I cannot think right anymore. Please help me to convert a interval like '2 days 00:22:10.2905' in seconds. Thank you very much in advance, Liviu ...
2
by: Zygo Blaxell | last post by:
I have a table with a few million rows of temperature data keyed by timestamp. I want to group these rows by timestamp intervals (e.g. every 32 seconds), compute aggregate functions on the...
2
by: Tom Allison | last post by:
I'm digging throught the data types and am trying to understand how to utilize the interval data type. I'm thinking I can use these values to add to another date field... Can someone give a...
13
by: Bruno Wolff III | last post by:
Recently there has been some discussion about attaching a timezone to a timestamp and some other discussion about including a 'day' part in the interval type. These two features impact each other,...
2
by: Ron St-Pierre | last post by:
I have a simple function which I use to set up a users' expiry date. If a field in a table contains an interval then this function returns a timestamp some time in the future (usually two weeks),...
0
by: kevinSQL | last post by:
Hi This is probably not very difficult but I can't seem to get it. I have a table with 2 colums. Type (int) and timestamp (datetime). A small snippet below. What I need to do is count number...
5
Plater
by: Plater | last post by:
So I have been working with the SNTP protocol (RFC4330) And it is saying that the timestamp format of 64bits is: . 1 2 3 0 1...
8
by: Phil Reynolds | last post by:
We are using SQL Server as a back end to an Access front end on a LAN using ODBC linked tables. Users are periodically getting the "data has been changed by another user" error, and it's causing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.