?
Solved

PHP: Counting minutes from dates in array

Posted on 2006-06-18
8
Medium Priority
?
259 Views
Last Modified: 2008-02-01
I have lot's of data printed out from oracle database to array. Some datarows have dates, some only zero.
So I have this kind of data:


02.06.2006 10:11:06
0
0
02.06.2006 20:42:52
03.06.2006 21:04:21
0
0
0
04.06.2006 13:57:28
0
0



So, how can I count minutes from those dates like this:
02.06.2006 10:11:06 - 02.06.2006 20:42:52     is xxx minutes
02.06.2006 20:42:52 - 03.06.2006 21:04:21     is xxx minutes
03.06.2006 21:04:21 - 04.06.2006 13:57:28     is xxx minutes


So those dates are some kind of startimes. And I need to count two different startimes different in minutes.

I can't do it myself :(
0
Comment
Question by:excinc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Accepted Solution

by:
merwetta1 earned 500 total points
ID: 16932771
try this...

<?php

## recreate your data array
$test_array[] = '02.06.2006 10:11:06';
$test_array[] = '0';
$test_array[] = '0';
$test_array[] = '02.06.2006 20:42:52';
$test_array[] = '03.06.2006 21:04:21';
$test_array[] = '0';
$test_array[] = '0';
$test_array[] = '0';
$test_array[] = '04.06.2006 13:57:28';
$test_array[] = '0';
$test_array[] = '0';


$time1 = 0;
foreach($test_array as $value)
{
      if ($value)
      {
            if ($time1)
            {
                  if (!$time2 = convert_time($value))  echo "invalid data found '$value'<br />";
                  else
                  {
                        $interval = ($time2 - $time1)/60;
                        echo "$value1 - $value is ".number_format($interval, 2).' minutes<br />';
                        $time1 = 0;
                  }
            } else
            {
                  $time1 = convert_time($value);
                  $value1 = $value;
            }
      }
}

function convert_time($time)
{
      if (!preg_match('/(\d{2})\.(\d{2})\.(\d{4}) (\d{2}):(\d{2}):(\d{2})/', $time, $matches))  return false;
      $time_unix = mktime($matches[4], $matches[5],  $matches[6],  $matches[1],  $matches[2],  $matches[3]);
      return $time_unix;
}

?>
0
 

Author Comment

by:excinc
ID: 16932891
I didn't get that work. It printed nothing :(


Actually when I have this kind of data printed out:

02.06.2006 10:11:06
0
0
02.06.2006 20:42:52
03.06.2006 21:04:21
0
0
0
04.06.2006 13:57:28



I need it to be listed excatly like this:

first group duration xxx min
0
0
second group duration xxx min
third group duration xxx min
0
0
0
04.06.2006 13:57:28
0
 
LVL 6

Expert Comment

by:merwetta1
ID: 16933083
that's odd. when i run it, i get:

02.06.2006 10:11:06 - 02.06.2006 20:42:52 is 631.77 minutes
03.06.2006 21:04:21 - 04.06.2006 13:57:28 is 44,153.12 minutes

the script could easily be modified to give you the exact output you're looking for, but since it's not working for you, that won't help. hopefully someone else can provide something that will work for you.
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 1

Expert Comment

by:FunnyMan
ID: 16933576
A PHP script that printed nothing is a strong indicator of a syntax error.  Check the error log (for me at least its /var/adm/httpd.error_log).
-FM
0
 

Author Comment

by:excinc
ID: 16933708
It works, sorry, my bad :)

Dates were in wrong format in array :-)
0
 

Author Comment

by:excinc
ID: 16933736
But, one thing...

That prints this like you said:


02.06.2006 10:11:06 - 02.06.2006 20:42:52 is 631.77 minutes
03.06.2006 21:04:21 - 04.06.2006 13:57:28 is 44,153.12 minutes



It should be like this:

02.06.2006 10:11:06 - 02.06.2006 20:42:52 is 631.77 minutes
02.06.2006 20:42:52 - 03.06.2006 21:04:21  time is where?????????????????????
03.06.2006 21:04:21 - 04.06.2006 13:57:28 is 44,153.12 minutes      should be 17h x 60min = about 1020min not 44k 153min????
0
 
LVL 1

Expert Comment

by:FunnyMan
ID: 16933877
To get the intermediate one, change:
$time1 = 0;
to
$time1 = $time2;


Looks like the answer was given in American date format (MM.DD.YYYY), so 03.06 is about a month (~43,000 min) after 02.06.

For European date format (DD.MM.YYYY), just change the last line from:
$time_unix = mktime($matches[4], $matches[5],  $matches[6],  $matches[1],  $matches[2],  $matches[3]);
     return $time_unix;
to
$time_unix = mktime($matches[4], $matches[5],  $matches[6],  $matches[2],  $matches[1],  $matches[3]);
     return $time_unix;

-FM
0
 
LVL 1

Expert Comment

by:FunnyMan
ID: 16933884
Okay, it's not quite the last line, but close.
-FM
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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