Jump to content


Photo

Manipulating Times


  • Please log in to reply
7 replies to this topic

#1 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 14 August 2006 - 08:12 PM

Hi,
I have run into a problem in a program I'm writing for my client... Heres the story:

I am developing a system that logs the downtime of aircraft due to many different reasons. One of them is maintenance. The maintenance work is based on a 24 hour day. Thus, the log is based on a 24 hour time scale where a user can log a downtime (ex: 05:00 to 14:30). However, the hours of operation for these aircraft is based on an 18 hour day (05:00 to 23:00). What I need to do is to be able to take a 24hr day maintenance log and convert it to an 18hr day to log the downtimes in operation.

Here is an example: Let's say that the aircraft is down for maintenance between the hours of 01:00 and 08:00. I need to convert this 24 hour scale to an 18 hour scale. In actuallity, I need to take the overlapping hours and store them in a variable. The hours of operation are 05:00 to 23:00, thus with the example of 01:00 to 08:00, I would count 05:00 to 08:00. How would I do that?

Its kinda confusing, I know, but any help is greatly appreciated!!!!

Thank you!
- Jeff

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 14 August 2006 - 08:14 PM

Store your hours as timestamps, and this will be easily achieved.
PHP Ninja

#3 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 14 August 2006 - 08:25 PM

And just so It's clear...I mean the hours that are submitted. Make sure they go into your database as a timestamp (11457688) , and not a formatted time (Such as 04:00 PM). You can do the formatting whenever you need to display or compare times.
PHP Ninja

#4 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 14 August 2006 - 08:31 PM

ok, so if the user had to enter the hours manually like "05:00" or something, how would I convert that to a timestamp? I still don't know how I would be able to do it, even if it is stored as a timestamp... Can you explain?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 14 August 2006 - 10:38 PM

try
<?php

$mnt_start = '18:00';
$mnt_end   = '23:30';

$op_start = '05:00';
$op_end   = '23:00';

$downtime_start = max($mnt_start, $op_start);
$downtime_end = min ($mnt_end, $op_end);

echo "$downtime_start - $downtime_end";
?> 

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 14 August 2006 - 11:26 PM

that works great! The only thing is that the minutes don't work... here is the code, I had to debug it a bit: Any reason why the minutes wouldn't work?

$mnt_start = '02:00';
$mnt_end   = '13:30';

$op_start = '05:00';
$op_end   = '23:00';

$downtime_start = max($mnt_start, $op_start);
$downtime_end = min($mnt_end, $op_end);

$display = ($downtime_end-$downtime_start);


#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 14 August 2006 - 11:44 PM

As a value like "08:30" is a string, iys numeric value is 8 (':' is non-numeric so anything after that is ignored);

If you want the downtime in hours:minutes, try

<?php
$mnt_start = '02:00';
$mnt_end   = '13:30';

$op_start = '05:00';
$op_end   = '23:00';

$downtime_start = max($mnt_start, $op_start);
$downtime_end = min($mnt_end, $op_end);

$dt_start = strtotime($downtime_start);
$dt_end = strtotime($downtime_end);

$mins = ($dt_end - $dt_start)/60;

$dt_hrs = floor($mins/60);
$dt_mins = $mins % 60;

echo $downtime_start, '-', $downtime_end, '<br>';
echo "Downtime : $dt_hrs:$dt_mins" ;
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 14 August 2006 - 11:59 PM

This is what I ended up with, and it works:  :) Thanks for your help!!!

$dt1 = "$dwntime_year $dwntime_month $dwntime_day";
$dt2 = str_replace(' ', '', $dt1);

$op_start = '05:00';
$op_end   = '23:00';

$downtime_start = max($dwntime_hrs1, $op_start);
$downtime_end = min($dwntime_hrs2, $op_end);

$dt3 = "$dt2 $downtime_start";
$dt4 = "$dt2 $downtime_end";

$downtime_hrs_start = strtotime($dt3);
$downtime_hrs_end = strtotime($dt4);

$dwntime_hrs = ($downtime_hrs_end-$downtime_hrs_start)/3600;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users