TIMESTAMP abd date diff

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

TIMESTAMP abd date diff

Post by apek »

i have to do a date different between two dates....
and my data type is in TIMESTAMP....

i try to do this:

Code: Select all

function tarikh($format,$mytimestamp)
{
   $month  = substr($mytimestamp,4,2);
   $day    = substr($mytimestamp,6,2);
   $year   = substr($mytimestamp,0,4);
   $epoch  = mktime(0,0,0,$month,$day,$year);
   $date   = date ($format, $epoch);
   return $date;
} 


$diff1=tarikh("m/d/Y",$bd["waktuMula"]);
$diff2=tarikh("m/d/Y",$bd["tarikhAduan"]);
    
	$beza=$diff1-$diff2;
but sometimes it gives me unreasonable output such as negative value...

and then i found this script:

Code: Select all

<?php
/*T.E.
07-Jan-2004 04:15
Most functions calculating the difference in days between two days take some assumptions. They often ignore leap-years or decide to set a month to 30 days. This one here should work better and more precise.
I left one thing to fix: I supposed a leap in every four years. This assumption is wrong when passing a xx00-border, if xx00 mod 400 != 0. However, PHPs date-function won't work with 1900 or less and 2100 or more. So for the next few years within the given boarders of PHP, this one should work.*/
function date_diff($dat1,$dat2)
/* Dat1 and Dat2 passed as "YYYY-MM-DD" */
{
  $tmp_dat1 = mktime(0,0,0,
     substr($dat1,5,2),substr($dat1,8,2),substr($dat1,0,4));
  $tmp_dat2 = mktime(0,0,0,
     substr($dat2,5,2),substr($dat2,8,2),substr($dat2,0,4));

  $yeardiff = date('Y',$tmp_dat1)-date('Y',$tmp_dat2);
  /* a leap year in every 4 years and the days-difference */
  $diff = date('z',$tmp_dat1)-date('z',$tmp_dat2) +
           floor($yeardiff /4)*1461;

  /* remainder */
  for ($yeardiff = $yeardiff % 4; $yeardiff>0; $yeardiff--)
   {
     $diff += 365 + date('L',
         mktime(0,0,0,1,1,
           intval(
             substr(
               (($tmp_dat1>$tmp_dat2) ? $dat1 : $dat2),0,4))
           -$yeardiff+1));
   }

  return $diff;
}
?>
but this one is giving me more unreasonable value....

can anyone help me to do date diff with TIMESTAMP datatype???
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Why not use MySQL's built in functions? There's even a DATEDIFF() one :o
http://www.mysql.com/doc/en/Date_and_ti ... tions.html
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

but the mysql DATEDIFF can bring negative value too....
thats why i dont use that....

mysql> SELECT DATEDIFF('1997-11-31 23:59:59','1997-12-31');
-> -30

even at the sample shown...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

SELECT ABS( TO_DAYS( '1997-11-31 23:59:59' ) - TO_DAYS( '1997-12-31' ) )

(or use DATEDIFF with ABS if you have MySQL => 4.1.1)
User avatar
apek
Forum Commoner
Posts: 96
Joined: Tue Jan 06, 2004 11:19 pm
Location: Holiday Plaza Johor Bahru

Post by apek »

the problem is i'm taking the date from the database...
and by query...
the query is join by some tables...
this is my full code:

Code: Select all

<?php
include("../inc/config.php");
include("../inc/dateandtime.php");
function query($query, $link, $line, $file)
{
	$result = mysql_query($query);
      if (empty($result) && ini_get('display_errors') == 1)
        {
            echo mysql_error().": ";
            echo mysql_errno()."<br />\n";
            echo $query."<br />\n";
            echo "@ $line in $file\n";
        }
	return $result;
}


?>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Zend Studio">
</HEAD>

<BODY>

<form name="myform" action="failureRate1.php">

<table width="100%" border="1" bgcolor="#666666" bordercolor="#333333" align="center">
  <tr> 
    <td align="center">
	  <font face="verdana" size="2" color="white"><b>
	  MEAN TIME TO FAILURE ANILISIS (MTTF)
	  </b></font>
	</td>
  </tr>
</table>

<?php 

$viewall1=query("Select * from repairbreakdown INNER JOIN aduankerosakan ON repairbreakdown.idAduan = aduankerosakan.idAduan WHERE aduankerosakan.noTag = '{$_POST["noTag"]}' and DATE_FORMAT(repairbreakdown.waktuTamat,'%Y') = '{$_POST["year"]}'", $link, __LINE__, __FILE__);
$bd=mysql_fetch_array($viewall1);
?>


<?php
if (empty($bd))
{
?>

<br>
<table width="100%" border="0"align="center">
  <tr> 
    <td align="center">
	  <font face="verdana" size="2" color="red"><b>
	  Maaf ! Tiada maklumat di dalam pangkalan data
	  </b></font>
	</td>
  </tr>
</table>
<P><BR>

<CENTER>
  <A HREF="MTTFMenu.php">
  <FONT FACE="VERDANA" SIZE="2"><STRONG>
    KEMBALI
  </STRONG></FONT>
  </A>
</CENTER>

<?php
}
  else
{


  $bil=0;
  $masaTidakDikenalPasti=0;
  $minitBD=0;
  $jamBD=0;
  $jamBD1=0;
  $masaBD=0;
  
  while($bd=mysql_fetch_array($viewall1))
  {
?>
<?php

$diff1=tarikh("m/d/Y",$bd["waktuMula"]);
$diff2=tarikh("m/d/Y",$bd["tarikhAduan"]);
    
	$beza=abs($diff1-$diff2);

    $minitBD=$minitBD+$bd["minitBD"];

    if ($minitBD>59)
    {


      $minitBD=$minitBD-60;
      $jamBD=$jamBD+1;

    }
      else
    {


      $minitBD=$minitBD;

    } 


    $jamBD=$jamBD+$bd["jamBD"]+($bd["hariBD"]*24);

    $masaTidakDikenalPasti=$masaTidakDikenalPasti+($beza*24);


    $bil=$bil+1;
  } 

?>

<?php 
  $masaBD1="".$jamBD.".".$minitBD."";
  $masaBD2=number_format($masaBD1, 2, '.', '');

  $masaOperasi=365*24;


  if ($minitBD<>0)
  {


    $masaOperasiBersih=$masaOperasi-1;
    $minitOperasiBersih=60-$minitBD;
    $jamOperasibersih=$masaOperasi-$jamBD;

    $masaOperasiBersih="".$jamOperasiBersih.".".$minitOperasiBersih."";

    $MTTF=($masaOperasiBersih-$masaTidakDikenalPasti)/$bil;
    $MTTF1=number_format($MTTF, 2, '.', '');

  }
    else
  {


    $MTTF=($masaOperasi-($masaBD2+$masaTidakDikenalPasti))/$bil;
    $MTTF1=number_format($MTTF, 2, '.', '');

  } 

?>
<br>

<table width="100%" border="0"align="center">
  <tr> 
    <td align="center">
	  <font face="verdana" size="2" color="red"><b>
	  Berikut adalah hasil analisa MTTF pada tahun <font color="black"><?php echo $_POST["year"]; ?></font> bagi mesin <font color="black"><?php   echo $_POST["noTag"]; ?></font>
	  </b></font>
	</td>
  </tr>
</table>
<br>
<TABLE BORDER="1" WIDTH="60%" BGCOLOR="#eeeeee" align=center cellPadding=0 cellSpacing=0  height=1 style="HEIGHT: 1px">
	
	<TR ALIGN="CENTER">
	  <TD ALIGN="CENTER" BGCOLOR="#666666" width="60%">
	    <font face="verdana" size="2" color="white"><strong> Item </strong></font>
	  </TD>
	  <TD ALIGN="CENTER" BGCOLOR="#666666" width="40%">
	    <font face="verdana" size="2" color="white"><strong> Maklumat </strong></font>
	  </TD>
	</TR>
	<TR>
	  <TD ALIGN="CENTER" BGCOLOR="#eeeeee" width="60%">
	    <font face="verdana" size="2" color="black"><strong>
	       Jumlah Masa Operasi
	    </font>
	  </TD> 
	  <TD><font face="verdana" size="2"><?php   echo $masaOperasi; ?>&nbsp;jam </font></TD>
	</TR>
	<TR>
	  <TD ALIGN="CENTER" BGCOLOR="#eeeeee" width="60%">
	    <font face="verdana" size="2" color="black"><strong>
	       Jumlah Masa Kegagalan Mesin
	    </font>
	  </TD> 
	  <TD><font face="verdana" size="2"><?php   echo $masaBD2; ?>&nbsp;jam </font></TD>
	</TR>
	<TR>
	  <TD ALIGN="CENTER" BGCOLOR="#eeeeee" width="60%">
	    <font face="verdana" size="2" color="black"><strong>
	       Jumlah Tidak DikenalPasti
	    </font>
	  </TD> 
	  <TD><font face="verdana" size="2"><?php   echo $masaTidakDikenalPasti; ?>&nbsp;jam </font></TD>
	</TR>
	<TR>
	  <TD ALIGN="CENTER" BGCOLOR="#eeeeee" width="60%">
	    <font face="verdana" size="2" color="black"><strong>
	       MTTF
	    </font>
	  </TD> 
	  <TD><font face="verdana" size="2"><b><?php   echo $MTTF1; ?>&nbsp;jam </b></font></TD>
	</TR>
</TABLE>	

<?php 
$insertdetail=query("SELECT * FROM MTTF WHERE tahun = '{$_POST["year"]}' AND noTag = '{$_POST["noTag"]}'", $link, __LINE__,__FILE__);
$insertMTTF=mysql_fetch_array($insertdetail);
?> 

<?php
if (empty($insertMTTF))
  {
$SQLinsert1=query("INSERT INTO MTTF(tahun,noTag,MTTFValue) VALUES('{$_POST["year"]}','{$_POST["noTag"]}','$MTTF1')", $link, __LINE__, __FILE__);
  }
    else
  {
$SQLupd=query("UPDATE MTTF SET MTTFValue = '$MTTF1' WHERE tahun = '{$_POST["year"]}' AND noTag = '{$_POST["noTag"]}' ", $link, __LINE__, __FILE__);
  } 
 ?>
 
<br>
<center>
  <a href="modelMain.php">
   <font face="verdana" color="black" size="2"><strong>
     KEMBALI
   </strong></font>
  </a> 
</center>


<? } ?>
</form>
</BODY>
</HTML>
if i put the abs at the $beza=abs($diff1-$diff2);
but can't the abs guarantee that the output is correct???
Post Reply