how to group timestamps by date?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkrubner@geocities.com

    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?

  • Micha³ Wo¼niak

    #2
    Re: how to group timestamps by date?

    One quick glance of an experienced eye allowed to understand the blurred
    and almost unreadable lkrubner@geocit ies.com's handwriting:
    [color=blue]
    > 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?[/color]

    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

    Comment

    • lkrubner@geocities.com

      #3
      Re: how to group timestamps by date?

      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?

      Comment

      • Daniel Tryba

        #4
        Re: how to group timestamps by date?

        lkrubner@geocit ies.com wrote:
        [smip][color=blue]
        > That would work, I think, but it seems slow and awkward. Surely there's
        > a more direct way of doing this?[/color]

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

        Comment

        • NC

          #5
          Re: how to group timestamps by date?

          lkrub...@geocit ies.com wrote:[color=blue]
          >
          > 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?[/color]

          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

          Comment

          • lkrubner@geocities.com

            #6
            Re: how to group timestamps by date?

            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.

            Comment

            • Daniel Tryba

              #7
              Re: how to group timestamps by date?

              lkrubner@geocit ies.com wrote:[color=blue]
              > 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.[/color]

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

              Comment

              • lkrubner@geocities.com

                #8
                Re: how to group timestamps by date?

                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?

                Comment

                Working...