473,218 Members | 1,353 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,218 software developers and data experts.

how to group timestamps by date?

This might be an idiot question, but how do you group by timestamps by
date? I mean, given a large number of timestamps, spanning many months,
how do grab them and say how many are from each day? If the timestamps
measure visits to a web site, how to easily say there were 45 visits on
January 4th?

The first idea that occurs to me is to put them all in an array and
then loop through the array and use date() on each, one at a time,
finding out the date of each and then adding up how many for each date.
But I'm wondering if PHP has a more direct, obvious command?

Jul 17 '05 #1
7 3888
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable lk******@geocities.com's handwriting:
mean, given a large number of timestamps, spanning many months,
how do grab them and say how many are from each day? If the timestamps
measure visits to a web site, how to easily say there were 45 visits on
January 4th?


Some brainstorming, maybe will lead in the right direction:

Timestamps are number of seconds from The Epoch (Jan 1st 1970, 00:00). So
$timestamp -> gives you the second
(int)($timestamp / 60) -> gives you the minute (from the Epoch!)
(int)($timestamp / (60 * 60)) -> gives you the hour (from the Epoch!)
(int)($timestamp / (60 * 60 * 24)) -> gives you the day (from the Epoch!)
and the tricky part starts here, as months have different numbers of
days.

As I said, only brainstorming here. I have no idea on how to manage the
months properly. :/

Cheers
Mike
Jul 17 '05 #2
This is a good line:

(int)($timestamp / (60 * 60 * 24)) -> gives you the day (from the
Epoch!)

Then I could round off and store the results in an array and then do
count by value on the array?

So, in theory, if I had an array like this:

$timestamps[] = 09876543
$timestamps[] = 09846543
$timestamps[] = 09836543
$timestamps[] = 09236543
$timestamps[] = 09106543
$timestamps[] = 09046543
$timestamps[] = 09016543

I could do something like this:
$countOfDays = array();
for ($i=0; $i < count($timestamps); $i++) {
$stamp = $timestamps[$i];
$day = $stamp / 86400;
$day = round($day);
$countOfDays[$day][] = $stamp;
}

reset($countOfDays);

while (list($key, $val) = each($countOfDays)) {
$count = count($dayArray);
$val = date("l dS of F Y h:i:s A", $val);
echo "On $val there were this many visits: $count";
}

That would work, I think, but it seems slow and awkward. Surely there's
a more direct way of doing this?

Jul 17 '05 #3
lk******@geocities.com wrote:
[smip]
That would work, I think, but it seems slow and awkward. Surely there's
a more direct way of doing this?


Where is are the timestamps coming from? Sound like a perfect job for a
RDMS.

Jul 17 '05 #4
NC
lkrub...@geocities.com wrote:

This might be an idiot question, but how do you group by
timestamps by date? I mean, given a large number of timestamps,
spanning many months, how do grab them and say how many are
from each day? If the timestamps measure visits to a web site,
how to easily say there were 45 visits on January 4th?


Assuming this is not a database question, here's an option:

$timestamps = array([many timestamps here]);
$visits = array();
foreach ($timestamps as $timestamp) {
$date = date('Y-m-d', $timestamp);
if (isset($visits[$date])) {
$visits[$date]++;
} else {
$visits[$date] = 1;
}
}
foreach ($visits as $date=>$number) {
echo "There were $number visits on $date.";
}

Cheers,
NC

Jul 17 '05 #5
I guess I could drag in MySql but I was hoping to keep it simple and
just use a flat file to store the dates.

Jul 17 '05 #6
lk******@geocities.com wrote:
I guess I could drag in MySql but I was hoping to keep it simple and
just use a flat file to store the dates.


You contradict yourself, see the hoops you have to jump through with a
flat text file, I wouldn't call that simple :)

Jul 17 '05 #7
Good point if one can assume the presence of a database. If there is no
database, then setting one up involves a lot of hoops.

Which leads to: What is the current status of SQLlite relative to PHP?
Is its use widespread?

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sugapablo | last post by:
From the manual, it seems negative timestamps should work on Linux, just not Windows. But anything I try before 1970, (i.e. a negative timestamp) is giving me 12/31/1969. Is this right? --
10
by: Craig Wahlmeier | last post by:
Am I the only one in the world that selects dates and timestamps with OLEDB? V8 of UDB has brought me a big problem. The PATCH2=24 setting no longer works the way it used to. The settings of...
5
by: Robert Schuldenfrei | last post by:
Dear NG, I have not heard from anyone about a good book that deals with the concurrency issue in SQL Server using C#. I have PROMISED Nick I would not use record locking and I have used an old...
4
by: Craig G | last post by:
im not too sure how i should be storing the SQL2000 timestamps basically i return a dataset which is used to populate an editable grid. this dataset contains the timestamp. how should i be...
4
by: Ying Lu | last post by:
Hello, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL does...
16
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want to my code to print the timestamps and each one to include...
1
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want my code to print the timestamps and each one to include...
3
by: scstrachan | last post by:
Does anyone know how to pull the date and the time from a timestamp field in msaccess? Here is an example. 8/30/2007 11:53:00 AM Ultimately, I would like to be able to place the date in one...
2
by: Kerryn | last post by:
Hi all new to this site so looking for some help. I am working in Access 2002, using a select query. I am trying to use a date range parameter to allow the users to end the start date and end...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.