By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

how to group timestamps by date?

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.