473,909 Members | 4,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3941
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable lk******@geocit ies.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)($timestam p / 60) -> gives you the minute (from the Epoch!)
(int)($timestam p / (60 * 60)) -> gives you the hour (from the Epoch!)
(int)($timestam p / (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)($timestam p / (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($timestam ps); $i++) {
$stamp = $timestamps[$i];
$day = $stamp / 86400;
$day = round($day);
$countOfDays[$day][] = $stamp;
}

reset($countOfD ays);

while (list($key, $val) = each($countOfDa ys)) {
$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******@geocit ies.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...@geocit ies.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******@geocit ies.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
2815
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
7178
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 MAPDATEDESCRIBE=1, MAPTIMEDESCRIBE=1, and MAPTIMESTAMPDESCRIBE=1 allow me to reverse that V8 change, almost. Before V8, the format of a date column returned in OLEDB was based upon the regional settings of the client. Now, it returns it in...
5
1503
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 row / new row approach. (see next paragraph for my reply to Nick) I would like to use Timestamps and Transactions to produce an iron-clad and efficient application in a multi-user environment. Can anyone recommend such a book? OK Nick, I...
4
2174
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 storing the timestamps for each grid row? is it possible to hold them in the grid in someway? or do i need to look at other means ive only used Oracle Timestamps before and they where just decimal timestamps which made it easy!! could just store them...
4
303
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 not support it. Could someone helps me please? The example table: T1 (col1 varchar(7) not null,
16
3076
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 hour:minute:second as of the corresponding old day. The only routine for this job seems to be localtime(timestmap) - this is Windows Vista, Visual Studio 2005 C++.
1
1811
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 hour:minute:second as of the corresponding old day. The only routine for this job seems to be localtime(timestmap) - this is Windows Vista, Visual Studio 2005 C++.
3
11974
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 field and the time in another. i would then like to be able to sort the time. Any help would be appreciated. Thanks,
2
2137
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 date i.e 1/1/08 and 31/1/08, the field name is Transaction Date and some dates have timestamps and some dont. Parameter >= And <= When entering the last date the query, if the transaction date record has a time stamp of 31/1/08 10:23 this is not...
0
10035
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10919
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7248
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5938
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3357
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.