Foros del Web » Programando para Internet » PHP »

Por QUE?

Estas en el tema de Por QUE? en el foro de PHP en Foros del Web. Helo estoy haciendo un proyecto que segun la hora inicial y la hora final me calcule el tiempo en dias laborables yo no lo hice ...
  #1 (permalink)  
Antiguo 04/06/2007, 07:55
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Por QUE?

Helo estoy haciendo un proyecto que segun la hora inicial y la hora final me calcule el tiempo en dias laborables yo no lo hice pero esta bueno ya he probado dos y estan super pero me sale este error en ambos
Fatal error: Cannot redeclare count_business_days() (previously declared in C:\AppServ\www\tracking\docs\member.php:347) in C:\AppServ\www\tracking\docs\member.php on line 347


el codigo es el siguiente

Código PHP:
<?php
// Set the default timezone to US/Eastern time:
date_default_timezone_set('US/Eastern');

// This function will count the number of business days between two dates

function count_business_days($a$b) {
    
// First, sort these.  We need to know which one comes first
    
if ($a $b) {
        
$first $a;
        
$second $b;
    } else {
        
$first $b;
        
$second $a;
    }

    
// Break these timestamps up into their constituent parts:
    
$f getdate($first);
    
$s getdate($second);

    
// Calculate the number of business days in the first week left.
    // Do this by subtracting the number of the day of the week from Friday
    
$f_days $f['wday'];
    
// If it was Saturday or Sunday you will get a -1 or 5 but we want 0
    
if (($f_days == 5) || ($f_days 0)) { $f_days 0; }

    
// Do the same for the second week except count to the beginning of
    // the week.  However, make sure that Saturday only counts as 5
    
$s_days = ($s['wday'] > 5) ? $s['wday'];

    
// Calculate the timestamp of midday, the Sunday after the first date:
    
$f_sunday mktime(1200$f['mon'],
                    
$f['mday'] + (($f['wday']) % 7), $f['year']);

    
// And the timestamp of midday, the Sunday before the second date:
    
$s_sunday mktime(1200$s['mon'],
                    
$s['mday'] - $s['wday'], $s['year']);

    
// Calculate the full weeks between these two dates by subtracting
    //  them, then dividing by the seconds in a week.  You need to round
    //  this afterwards to always ensure an even number.  Otherwise
    //  daylight savings time can offset the calculation.
    
$weeks round(($s_sunday $f_sunday) / (3600*24*7));

    
// Return the number of days by multiplying weeks by 5 and adding
    //  the extra days:
    
return ($weeks 5) + $f_days $s_days;
}

// Try a couple of examples:
$date1 strtotime('5/24/2007 9:13am');
$date2 strtotime('5/31/2007 6:15pm');

// Calculate the business days between, They are: 31 & 8109
echo "<p>There are "count_business_days($date1$date2), " days.</p>";
?>
el obtetivo de este script es que se repita con el horario laboral de cada dia please ya no se que hacer heeeeeeeeeeeeeeeeeeelp
  #2 (permalink)  
Antiguo 04/06/2007, 08:08
 
Fecha de Ingreso: mayo-2007
Ubicación: la mancha
Mensajes: 11
Antigüedad: 16 años, 11 meses
Puntos: 0
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

El codigo php está bien, y el error que te muestra parece referente a la llamada de la función, como llamas a la función para que se ejecute?
  #3 (permalink)  
Antiguo 04/06/2007, 08:13
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

El PHP no está bien.
Código PHP:
echo "<p>There are "count_business_days($date1$date2), " days.</p>"


debería ser
Código PHP:
echo "<p>There are "count_business_days($date1$date2). " days.</p>"


Es decir, el operador de concatenación no es la coma, sino el punto.


Saludos.
  #4 (permalink)  
Antiguo 04/06/2007, 08:29
Avatar de izar  
Fecha de Ingreso: enero-2004
Mensajes: 519
Antigüedad: 20 años, 2 meses
Puntos: 1
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

Solo una sugerencia. La próxima vez podrías poner al post un título algo más descriptivo?. Gracias
  #5 (permalink)  
Antiguo 04/06/2007, 08:46
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

ok parece que no me explique bien la funcion si la verifican no tiene ningn problema y lso calculos los hace super bien, yo esty haciendo un loop de esa funcion por eso es que me da el error
  #6 (permalink)  
Antiguo 04/06/2007, 08:53
(Desactivado)
 
Fecha de Ingreso: diciembre-2006
Mensajes: 529
Antigüedad: 17 años, 4 meses
Puntos: 11
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

El error te lo tira en la linea 347 y el código que posteaste no tiene ni 60 lineas.

Está incompleto y asi no se puede apreciar el error.

Básicamente lo que el error te esta diciendo es de que alguna manera estas declarando la función dos veces de la forma:

Código:
 
function count_business_days...
Pero sin el código completo no se puede hacer mucho.

SAludos
  #7 (permalink)  
Antiguo 04/06/2007, 09:05
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

Creo es bastante obvio, si tu defines tu funcion DENTRO del ciclo, la primera vez no pasa nada, pero a las siguientes te va a tirar ese error, porque ya esta definida la funcion, tienes que definir tu funcion fuera del ciclo.

Saludos.
  #8 (permalink)  
Antiguo 04/06/2007, 09:11
(Desactivado)
 
Fecha de Ingreso: diciembre-2006
Mensajes: 529
Antigüedad: 17 años, 4 meses
Puntos: 11
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

ahh es un ciclo??
Claro, TIENE que se un ciclo porque según el error la redeclaración de la función es en la misma linea de la misma página.

Bien GatorV!
  #9 (permalink)  
Antiguo 04/06/2007, 10:25
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

si es un ciclo que puedo hacer entonces ???
  #10 (permalink)  
Antiguo 04/06/2007, 10:38
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

Ya te dije, sacalo del ciclo, si lo tienes asi:
Código PHP:
while( $ciclo ) {
    function 
bla() {

    }

Sacalo asi:
Código PHP:
function blah() {

}
while( 
$ciclo ) {
    
blah();

  #11 (permalink)  
Antiguo 04/06/2007, 10:44
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

ok hasta ahora tanbto ustedes como yo estamos por ambos caminos pero no me acaban de entender les exlico mi proyecto

a:una pagina de busqueda cuando ponen el nombre del trabajador

b:una pagina de resultados donde sale la informacion del trabajador y los horarios de entrada y salida.


ok hasta ahi bien lo que quiero es que (en un recordset que hice) me muestre al lado de cada dia (Hora de entrada y hora de salida) el tiempo que trabajo o dias laborables que estuvo para terminar su trabajo como es un ciclo me sale ese error no le veo sentido a sacar ese escript del ciclo pq ahi es donde me hace falta utilizarlo estoy tratando de buscar uan solucion please gracias a todos los que estan colaborando
  #12 (permalink)  
Antiguo 04/06/2007, 10:49
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

Te repito, tienes que sacar la DEFINICIÓN e IMPLEMENTACIÓN de la funcion FUERA del CICLO, si no te va a tirar ese error, si tu pones la DEFINICIÓN e IMPLEMENTACIÓN al principio/final del script, no vas a tener ningún problema y vas a poder realizar tu ciclo y calcular los dias que estuvo.

Saludos.
  #13 (permalink)  
Antiguo 04/06/2007, 11:10
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

ok saque la funcion fuera del while y solo deje los echo dentro. por ahora sin error pero ................... solo me calcula el 1er record que hago
  #14 (permalink)  
Antiguo 04/06/2007, 11:20
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

Amigo puedo estar tratando de adivinar lo que estas haciendo, o mas fácil puedes publicar el código y asi te podría decir.

Saludos.
  #15 (permalink)  
Antiguo 04/06/2007, 11:26
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

Cita:
Iniciado por santuariosw Ver Mensaje
el codigo es el siguiente

Código PHP:
<?php
// Set the default timezone to US/Eastern time:
date_default_timezone_set('US/Eastern');

// This function will count the number of business days between two dates

function count_business_days($a$b) {
    
// First, sort these.  We need to know which one comes first
    
if ($a $b) {
        
$first $a;
        
$second $b;
    } else {
        
$first $b;
        
$second $a;
    }

    
// Break these timestamps up into their constituent parts:
    
$f getdate($first);
    
$s getdate($second);

    
// Calculate the number of business days in the first week left.
    // Do this by subtracting the number of the day of the week from Friday
    
$f_days $f['wday'];
    
// If it was Saturday or Sunday you will get a -1 or 5 but we want 0
    
if (($f_days == 5) || ($f_days 0)) { $f_days 0; }

    
// Do the same for the second week except count to the beginning of
    // the week.  However, make sure that Saturday only counts as 5
    
$s_days = ($s['wday'] > 5) ? $s['wday'];

    
// Calculate the timestamp of midday, the Sunday after the first date:
    
$f_sunday mktime(1200$f['mon'],
                    
$f['mday'] + (($f['wday']) % 7), $f['year']);

    
// And the timestamp of midday, the Sunday before the second date:
    
$s_sunday mktime(1200$s['mon'],
                    
$s['mday'] - $s['wday'], $s['year']);

    
// Calculate the full weeks between these two dates by subtracting
    //  them, then dividing by the seconds in a week.  You need to round
    //  this afterwards to always ensure an even number.  Otherwise
    //  daylight savings time can offset the calculation.
    
$weeks round(($s_sunday $f_sunday) / (3600*24*7));

    
// Return the number of days by multiplying weeks by 5 and adding
    //  the extra days:
    
return ($weeks 5) + $f_days $s_days;
}

// Try a couple of examples:
$date1 strtotime('5/24/2007 9:13am');
$date2 strtotime('5/31/2007 6:15pm');

// Calculate the business days between, They are: 31 & 8109
echo "<p>There are "count_business_days($date1$date2), " days.</p>";
?>
el obtetivo de este script es que se repita con el horario laboral de cada dia please ya no se que hacer heeeeeeeeeeeeeeeeeeelp
  #16 (permalink)  
Antiguo 04/06/2007, 11:31
Avatar de Nefertiter  
Fecha de Ingreso: enero-2003
Ubicación: Rosario
Mensajes: 1.316
Antigüedad: 21 años, 3 meses
Puntos: 9
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

ese codigo esta bien, publica como lo usas.

Cita:
Es decir, el operador de concatenación no es la coma, sino el punto.
la coma tambien sirve para concatenar durante la impresion.
  #17 (permalink)  
Antiguo 04/06/2007, 11:55
(Desactivado)
 
Fecha de Ingreso: diciembre-2006
Mensajes: 529
Antigüedad: 17 años, 4 meses
Puntos: 11
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

No me detuve mucho... pero me parece que el último código es el mismo de arriba???? no tiene cambios ...

Última edición por usermax; 04/06/2007 a las 12:22
  #18 (permalink)  
Antiguo 04/06/2007, 12:04
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

si man es el mismo ya no se ni que hacer
  #19 (permalink)  
Antiguo 04/06/2007, 12:11
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Re: Por QUEEEEEEEEEEEEEEEEEEEEE ??

Si publicas el código de member.php lineas 200 - 400 te puedo ayudar, ese código que estas poniendo arriba no es el código que estas utilizando en tu IMPLEMENTACIÓN.
  #20 (permalink)  
Antiguo 04/06/2007, 15:58
 
Fecha de Ingreso: diciembre-2006
Ubicación: Miami
Mensajes: 55
Antigüedad: 17 años, 4 meses
Puntos: 0
Solucion

Gracias a un colega de cubatravel en cuba y a todos ustedes se pudo resolver esto, todos ustedes tenian razon en lo que me decian pero cuando no mke daba error solo me calculaba el 1er registro bueno sin mas muela la sulucion es sacar esto fuera del loop
Código PHP:
<?php
// Set the default timezone to US/Eastern time:
date_default_timezone_set('US/Eastern');

// This function will count the number of business days between two dates

function count_business_days($a$b) {
    
// First, sort these.  We need to know which one comes first
    
if ($a $b) {
        
$first $a;
        
$second $b;
    } else {
        
$first $b;
        
$second $a;
    }

    
// Break these timestamps up into their constituent parts:
    
$f getdate($first);
    
$s getdate($second);

    
// Calculate the number of business days in the first week left.
    // Do this by subtracting the number of the day of the week from Friday
    
$f_days $f['wday'];
    
// If it was Saturday or Sunday you will get a -1 or 5 but we want 0
    
if (($f_days == 5) || ($f_days 0)) { $f_days 0; }

    
// Do the same for the second week except count to the beginning of
    // the week.  However, make sure that Saturday only counts as 5
    
$s_days = ($s['wday'] > 5) ? $s['wday'];

    
// Calculate the timestamp of midday, the Sunday after the first date:
    
$f_sunday mktime(1200$f['mon'],
                    
$f['mday'] + (($f['wday']) % 7), $f['year']);

    
// And the timestamp of midday, the Sunday before the second date:
    
$s_sunday mktime(1200$s['mon'],
                    
$s['mday'] - $s['wday'], $s['year']);

    
// Calculate the full weeks between these two dates by subtracting
    //  them, then dividing by the seconds in a week.  You need to round
    //  this afterwards to always ensure an even number.  Otherwise
    //  daylight savings time can offset the calculation.
    
$weeks round(($s_sunday $f_sunday) / (3600*24*7));

    
// Return the number of days by multiplying weeks by 5 and adding
    //  the extra days:
    
return ($weeks 5) + $f_days $s_days;
}
?>
y dentro del loop eso

Código PHP:
<?php
// Try a couple of examples:
$date1 strtotime($variable de fechainicial);
$date2 strtotime($variable de fechafinal);
// Calculate the business days between, They are: 31 & 8109
echo "<p>There are "count_business_days($date1$date2), " days.</p>";
?>
el problema que me daba aparte de no redeclarar la funcion era que yo estaba poniendo

Código PHP:

$date1 
date ("m/d/Y h:ja"strtotime($variable de fechainicial));
$date2 date ("m/d/Y h:ja"strtotime($variable de fechafinal); 
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:59.