473,386 Members | 1,736 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,386 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 3911
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.