Está usted en Indice > Construcción > Lenguajes > PHP > Lecciones y Paso a Paso > Calculando el tiempo que media entre dos timestamps en PHP
Construcción
Maletín
Utilidades
Cursos
Promoción
Rentabilidad
Zona Novatos
Foros
Acceso a tu cuenta

Calculando el tiempo que media entre dos timestamps en PHP

<?php

function  timespan ( $start , $end ) {
//This function provides a way to return back the number of
     //years,months,weeks,days,hours,minutes and seconds from
     //a starting timestamp as $start, to a ending timestamp as $end.
if((! strtotime ( $start ) || ! strtotime ( $end )) && (! is_numeric ( $start ) || ! is_numeric ( $end )))
         {
         die( "Wrong datatype for timespan() on Line: " . __LINE__ . " in File: " . __FILE__ );
         }
     if(! is_numeric ( $start ))  $start  =  strtotime ( $start );
     if(! is_numeric ( $end ))  $end  =  strtotime ( $end );
$span  =  $end - $start ;

// WEEK
$return [ "week" ] =  0 ;
     if( $span  >  604800 )
         {
$return [ "week" ] =  intval ( intval ( $span ) /  604800 );
$span  =  intval ( intval ( $span ) - ( $return [ "week" ] *  604800 ));
         }

// DAY
$return [ "day" ] =  0 ;
     if( $span  >  86400 )
         {
$return [ "day" ] =  intval ( intval ( $span ) /  86400 );
$span  =  intval ( intval ( $span ) - ( $return [ "day" ] *  86400 ));
         }

// HOUR
$return [ "hour" ] =  0 ;
     if( $span  >  3600 )
         {
$return [ "hour" ] =  intval ( intval ( $span ) /  3600 );
$span  =  intval ( intval ( $span ) - ( $return [ "hour" ] *  3600 ));
         }

// MINUTE
$return [ "minute" ] =  0 ;
     if( $span  >  60 )
         {
$return [ "minute" ] =  intval ( intval ( $span ) /  60 );
$span  =  intval ( intval ( $span ) - ( $return [ "minute" ] *  60 ));
         }

// SECOND
$return [ "second" ] =  0 ;
     if( $span  >  0 )
         {
$return [ "second" ] =  $span ;
         }
     return  $return ;


// ejemplo: calculemos tiempo que media entre el 14 de febrero de 2005 
// y el 13 de Marzo de 2005:


$start  =  mktime ( 0 , 0 , 0 , 2 , 14 , 2005 );
$end  =  mktime ( 0 , 0 , 0 , 3 , 13 , 2005 );

print_r  ( timespan ( $start , $end )); 
//Array ( [week] => 3 [day] => 6 [hour] => 0 [minute] => 0 [second] => 0 )

?>




Autor: phpexamples.net
http://www.phpexamples.net/content/blogsection/5/28/9/9/

Usuarios que han visto este tema también han visto...

- Cookies en PHP
- Tiempo de ejecución de una página con PHP
- Restringir el acceso a un area con PHP
- Recortar texto en PHP
- Selects combinados con Ajax y PHP


Versión imprimible - Versión imprimible de este documento
Enviar e-mail - Enviar por e-mail este documento
Publicidad






Cursos de Community Manager

Información legal | Política de Privacidad | Contacte con nosotros

Otro proyecto de Factoría de Internet. Copyright© 2003-2011 Factoría de Internet S.L.. Todos los derechos reservados.


Página generada el 25-05-2012 a las 23:51:38