listo resuelto el problema ya funciona pero tiene dos defectos
1 en el codigo yo concateno una frase de texto con el resultado de tres variables esto lo guardo en la variable $TiempoDeUso deberia guardar eu texto como este:
"En el inventario, El tiempo de uso es de: 16 años 2 meses 28 dias"
pero en lugar de eso guarda en la base de datos el valor 5155 """UN NUMERO"" no se por que...
2- el codigo lo puse dentro de un do while para que corra el codigo para todos los registros de la tabla activo..
empiesa bien pero se queda 30 segundos haciendolo y manda este mensaje:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\SARA_WEB\pruebafechas.php on line113
supongo que si resuelvo el problema 1 entonces puedo poner este codigo en una funcion en la base de datos y hacer que se ejecute una vez al dia...(ESPERO QUE ESO SE PUEDA HACER EN MYSQL)
pego aqui el codigo ya corregidopra aquellos que son nuevos como yo principalmente y para todo aque que necesite un ejemplo de manejo de fechas, variables consultas y Update a la base de datos asi como el uso de operaciones aritmeticas.
basicamente lo que hace es cojer los datos de una tabla llamada activos y le calcula la depreciacion y el tiempo de uso del activo desde su fecha de compra hasta el momento, y despues actualiza la misma tabla con los nuevos valores
perdon si el codigo se desformatea al pegarlo pero no se como hacer para que se quede intacto
Código PHP:
Ver original<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
}
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ?
intval($theValue) : "NULL"; break;
case "double":
$theValue = ($theValue != "") ?
doubleval($theValue) : "NULL"; break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$query_ConsultaActivos = "SELECT * FROM activos";
/*$Porcentaje = $_POST["Porc"];*/
/*$ValorActual = $_POST["ValorActual"];*/
do{
$Porcentaje =$row_ConsultaActivos['PORCDEP'];
$ValorActual =$row_ConsultaActivos['VALORACTUAL'];
$Secuencia = $row_ConsultaActivos['SECUENCIA'];
$ValorAgregado = $row_ConsultaActivos['VALORAGREGADO'];
$Valor = $row_ConsultaActivos['VALOR'];
if (($Porcentaje)> 0){
$AñoAct= date("Y", $date); $MesAct= date("m", $date); $DiaAct= date("d", $date);
$date7 = strtotime($row_ConsultaActivos['FECHAADQUISICION']); $AñoAdq= date("Y", $date7); $MesAdq= date("m", $date7); $DiaAdq= date("d", $date7);
if(($DiaAct)< $DiaAdq){
$MesAct= $MesAct - 1;
$DiaAct = $DiaAct +30;
}
if(($MesAct)< $MesAdq){
$AñoAct= $AñoAct - 1;
$MesAct = $MesAct +12;
}
$Dia = $DiaAct - $DiaAdq;
$Mes = $MesAct - $MesAdq;
$Año = $AñoAct - $AñoAdq;
$TiempoDeUso = ( "En el inventario, El tiempo de uso es de: ". $Año ." años ".$Mes . " meses ". $Dia. " dias ");
echo $TiempoDeUso;
echo $row_ConsultaActivos['SECUENCIA'];
$AñosEnDias = $Año * 365;
$MesesEnDias = $Mes * 30;
$TiempoDeUso = $AñosEnDias + $MesesEnDias + $Dia;
//==============================================
// SI EL VALOR ACTUAL ES MAYOR O IGUAL A 100
//==============================================
if (($ValorActual)>= 100) {
$depacum = $row_ConsultaActivos['TOTALDEPRECIADO'];
$DepAnual=(($ValorActual * $Porcentaje)/100)* $Año;
$DepMen = ((($ValorActual * $Porcentaje)/100)/12)* $Mes;
$DepDia = (((($ValorActual * $Porcentaje)/100)/12)/30)* $Dia;
$DepTotal = $DepAnual + $DepMen + $DepDia;
$ValorReal = $ValorActual - $DepTotal;
$NewVal= $ValorActual - $DepTotal;
if (($NewVal)<= 1){
$NewVal = 1;
$TotalDepreciado = $ValorActual - 1;
}else{
$TotalDepreciado = $DepTotal;
}
$RestaAcumulada = $depacum - $DepTotal;
//==============================================
// ACTUALIZACION LOS DATOS DE LA TABLA ACTIVOS
//==============================================
$updateSQL = sprintf("UPDATE activos SET DEPRECIACIONACUMULADA=%s, VALORREAL=%s, VALORDEPRECANUAL=%s, VALORDEPRECMENS=%s, VALORDEPRECDIARIO=%s, FECHAACTUAL=%s, TOTALDEPRECIADO=%s, TIEMPOUSO=%s WHERE SECUENCIA=%s", GetSQLValueString($DepTotal, "double"),
GetSQLValueString($NewVal, "double"),
GetSQLValueString($DepAnual, "double"),
GetSQLValueString($DepMen, "double"),
GetSQLValueString($DepDia, "double"),
GetSQLValueString
(date("Y-m-d"), "date"), GetSQLValueString($TotalDepreciado, "double"),
GetSQLValueString($TiempoDeUso, "text"),
GetSQLValueString($Secuencia, "text"));
/*mysql_query("SET NAMES 'utf-8'");*/
if(($DepTotal)>$ValorAgregado){
$TotalDepreciado = ($Valor + $DepTotal)-1;
$updateSQL1 = sprintf("UPDATE activos SET TOTALDEPRECIADO=%s WHERE SECUENCIA=%s", GetSQLValueString($TotalDepreciado, "double"),
GetSQLValueString($Secuencia, "text"));
}
if (($RestaAcumulada)<> 0){
$DepreciacionAcumulada = $depacum + $DepDia;
$updateSQL2 = sprintf("UPDATE activos SET DEPRECIACIONACUMULADA=%s WHERE SECUENCIA=%s", GetSQLValueString($DepreciacionAcumulada, "double"),
GetSQLValueString($Secuencia, "text"));
}
//==============================================
// SI EL VALOR ACTUAL ES MENOR A 100
//==============================================
}
if (($ValorActual)< 100) {
$DepDia = $ValorActual / 365 ;
$DepTotal=$TiempoDeUso * $DepDia;
$NewVal = $ValorActual - $DepTotal;
if (($NewVal)<= 0){
$NewVal = 1;
$TotalDepreciado = $ValorActual - 1;
}else{
$TotalDepreciado = $DepTotal;
}
$updateSQL = sprintf("UPDATE activos SET TOTALDEPRECIADO=%s, VALORREAL=%s, VALORDEPRECDIARIO=%s, FECHAACTUAL=%s, TIEMPOUSO=%s WHERE SECUENCIA=%s", GetSQLValueString($TotalDepreciado, "double"),
GetSQLValueString($NewVal, "double"),
GetSQLValueString($DepDia, "double"),
GetSQLValueString($Fecha1MySQL, "date"),
GetSQLValueString($TiempoDeUso, "text"),
GetSQLValueString($Secuencia, "text"));
/*mysql_query("SET NAMES 'utf-8'");*/
}
}
if($rows > 0) {
}
?>