?
Solved

PHP working with dates...

Posted on 2012-09-02
7
Medium Priority
?
705 Views
Last Modified: 2012-09-02
Hi, I have the list of dates... For example:


08-31-12 11:34
08-31-12 11:31
08-31-12 11:26
08-31-12 11:20
08-29-12 15:13
08-27-12 12:13
08-29-12 11:13

I get those dates from foreach loop:

foreach ($matches[1] as $num => $value) {
$tmp_date             = $matches[3][$num];
}

One date equals to an operation done that day.
Now I need to somehow count, how many operations were done each day, considering that some days there may have been 100 operations and days may have been none.
0
Comment
Question by:VAL1N
  • 4
  • 2
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38358926
You can use the count() function to get the number of elements in an array.
http://us2.php.net/manual/en/function.count.php
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38358929
Just a thought... If your code is generating the list of dates, use the ISO-8601 representation for the dates.  Over time you will find that this is a lot easier to read and understand, and it is unambiguous.
0
 

Author Comment

by:VAL1N
ID: 38358932
Yea, but there is no array, the data comes from html page, and I need to somehow count how many times on that page, I see every one of those dates...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 38358934
Build an array on the fly, loop over it at the end to report all dates and counts:

$dates = array();
foreach ($matches[1] as $num => $value) {
  $tmp_date = $matches[3][$num];
  $idx = date('Y-m-d',strtotime($tmp_date));
  if(isset($dates[$idx]))
    $dates[$idx]++;
  else $dates[$idx] = 1;
}
foreach($dates as $date=>$count)
  echo $date.': '.$count.'<br />';

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38358938
See http://www.laprbass.com/RAY_temp_val1n.php

I changed the format of the dates from m-d-y to Y-m-d.  The latter format is in consonance with the ISO-8601 standard for dates.  Without this change, strtotime() cannot process the date/time string.  If you had to make this change programmatically, it would require you to do some additional string manipulation.  It's better to just use the ISO standard format for all internal representations of date and time.

<?php // RAY_temp_val1n.php
error_reporting(E_ALL);
echo '<pre>';

// FROM THE POST AT EE
$dates = <<<DATES
2012-08-31 11:34
2012-08-31 11:31
2012-08-31 11:26
2012-08-31 11:20
2012-08-29 15:13
2012-08-27 12:13
2012-08-29 11:13
DATES;

// REQUIRED PHP 5.1+
date_default_timezone_set('America/Chicago');

// AN ARRAY OF THE DATES
$arr = explode(PHP_EOL, $dates);

// AN ARRAY OF COUNTS
$out = array();

// LOWER THE ERROR REPORTING SO WE CAN USE UNDEFINED ARRAY POSITIONS AS IF THEY CONTAINED ZERO
error_reporting(E_ALL ^ E_NOTICE);

// COUNT THE DAYS
foreach ($arr as $date_thing)
{
    $date_key = date('Y_m_d', strtotime($date_thing));
    $out[$date_key]++;
}

// SHOW THE WORK PRODUCT
print_r($out);

Open in new window

HTH, ~Ray
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38358939
the data comes from html page
OK, then post a link to the HTML page so we can see the real test data!
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 38359097
...sigh... now I remember why I lost interest in this site.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question