<?php
/*
getTimeElapsed
==============
(C/W) CI:A - Certus In Audio [http://certus.in/]
A very, very, ..., simple function to return elapsed time as strings.
It is just a FUNCTION EXAMPLE, and maybe you will find it useful.
Feel free to use it (on your own risk, for sure).
HAVE FUN! :-D
Sourced by CI:A - Certus In Audio [http://certus.in/].
USAGE:
Please have a look at the following example code...
$arrLabelSets = array(
'en' => array( 'y' => 'year' ,
'm' => 'month' ,
'w' => 'week' ,
'd' => 'day' ,
'h' => 'hour' ,
'i' => 'minute' ,
's' => 'second' ,
'ps' => 's' ,
'px' => 'just now' ) ,
'de' => array( 'y' => 'Jahr(e)' ,
'm' => 'Monat(e)' ,
'w' => 'Woche(n)' ,
'd' => 'Tag(e)' ,
'h' => 'Stunde(n)' ,
'i' => 'Minute(n)' ,
's' => 'Sekunde(n)' ,
'ps' => '' ,
'px' => 'gerade jetzt' )
);
$intDemoTimesTS = rand( 0 , 2 );
$arrDemoTimes = array(
'DemoTime1' => array(
'year' => ( intval( date( 'Y' ) ) - $intDemoTimesTS ) ,
'month' => ( intval( date( 'm' ) ) - $intDemoTimesTS ) ,
'day' => ( intval( date( 'd' ) ) - $intDemoTimesTS ) ,
'hour' => ( intval( date( 'H' ) ) - $intDemoTimesTS ) ,
'minute' => ( intval( date( 'i' ) ) - $intDemoTimesTS ) ,
'second' => ( intval( date( 's' ) ) - $intDemoTimesTS ) ) ,
'DemoTime2' => array(
'y' => ( intval( date( 'Y' ) ) - $intDemoTimesTS ) ,
'm' => ( intval( date( 'm' ) ) - $intDemoTimesTS ) ,
'd' => ( intval( date( 'd' ) ) - $intDemoTimesTS ) ,
'h' => ( intval( date( 'H' ) ) - $intDemoTimesTS ) ,
'i' => ( intval( date( 'i' ) ) - $intDemoTimesTS ) ,
's' => ( intval( date( 's' ) ) - $intDemoTimesTS ) ) ,
'DemoTime3' => mktime(
( intval( date( 'H' ) ) - $intDemoTimesTS ) ,
( intval( date( 'i' ) ) - $intDemoTimesTS ) ,
( intval( date( 's' ) ) - $intDemoTimesTS ) ,
( intval( date( 'm' ) ) - $intDemoTimesTS ) ,
( intval( date( 'd' ) ) - $intDemoTimesTS ) ,
( intval( date( 'Y' ) ) - $intDemoTimesTS ) ) ,
'DemoTime4' => mktime(
( intval( date( 'H' ) ) - $intDemoTimesTS ) ,
( intval( date( 'i' ) ) - $intDemoTimesTS ) ,
( intval( date( 's' ) ) - $intDemoTimesTS ) ,
( intval( date( 'm' ) ) - $intDemoTimesTS ) ,
( intval( date( 'd' ) ) - $intDemoTimesTS ) ,
( intval( date( 'Y' ) ) - $intDemoTimesTS ) ) ,
'DemoTime5' =>
( intval( date( 'Y' ) ) - $intDemoTimesTS ) . '-' .
( intval( date( 'm' ) ) - $intDemoTimesTS ) . '-' .
( intval( date( 'd' ) ) - $intDemoTimesTS ) . ' ' .
( intval( date( 'H' ) ) - $intDemoTimesTS ) . ':' .
( intval( date( 'i' ) ) - $intDemoTimesTS ) . ':' .
( intval( date( 's' ) ) - $intDemoTimesTS ) ,
'DemoTime6' =>
( intval( date( 'Y' ) ) - $intDemoTimesTS ) . '-' .
( intval( date( 'm' ) ) - $intDemoTimesTS ) . '-' .
( intval( date( 'd' ) ) - $intDemoTimesTS ) . ' ' .
( intval( date( 'H' ) ) - $intDemoTimesTS ) . ':' .
( intval( date( 'i' ) ) - $intDemoTimesTS ) . ':' .
( intval( date( 's' ) ) - $intDemoTimesTS )
);
header( 'Content-Type: text/plain' );
foreach( $arrLabelSets as $strLSK => $arrLSV )
{
echo strtoupper( $strLSK ) . PHP_EOL;
foreach( $arrDemoTimes as $strDTK => $mxDTV )
{
echo $strDTK . ' > ' . getTimeElapsed( $mxDTV , false , $arrLSV ) .
PHP_EOL . ' ' .
str_replace(
array( PHP_EOL , ' ' , 'Array' ) ,
array( '' , ' ' , '' ) ,
print_r( getTimeElapsed( $mxDTV , true ) , true ) ) .
PHP_EOL . PHP_EOL;
}
echo PHP_EOL;
}
*/
function getTimeElapsed( $mxDateTime ,
$bReturnAsArray = null ,
$arrLabelSet = null )
{
if( is_array( $mxDateTime ) )
{
$mxDateTime = array_change_key_case( $mxDateTime , CASE_LOWER );
$mxDateTime = ( ( isset( $mxDateTime[ 'year' ] ) ) ?
$mxDateTime[ 'year' ] :
( ( isset( $mxDateTime[ 'y' ] ) ) ?
$mxDateTime[ 'y' ] :
date( 'Y' ) ) ) . '-' .
( ( isset( $mxDateTime[ 'month' ] ) )
? $mxDateTime[ 'month' ] :
( ( isset( $mxDateTime[ 'm' ] ) ) ?
$mxDateTime[ 'm' ] :
date( 'm' ) ) ) . '-' .
( ( isset( $mxDateTime[ 'day' ] ) ) ?
$mxDateTime[ 'day' ] :
( ( isset( $mxDateTime[ 'd' ] ) ) ?
$mxDateTime[ 'd' ] :
date( 'd' ) ) ) . ' ' .
( ( isset( $mxDateTime[ 'hour' ] ) ) ?
$mxDateTime[ 'hour' ] :
( ( isset( $mxDateTime[ 'h' ] ) ) ?
$mxDateTime[ 'h' ] :
date( 'H' ) ) ) . ':' .
( ( isset( $mxDateTime[ 'minute' ] ) ) ?
$mxDateTime[ 'minute' ] :
( ( isset( $mxDateTime[ 'i' ] ) ) ?
$mxDateTime[ 'i' ] :
date( 'i' ) ) ) . ':' .
( ( isset( $mxDateTime[ 'second' ] ) ) ?
$mxDateTime[ 'second' ] :
( ( isset( $mxDateTime[ 's' ] ) ) ?
$mxDateTime[ 's' ] :
date( 's' ) ) );
}
elseif( is_integer( $mxDateTime ) )
{
$mxDateTime = date( 'Y-m-d H:i:s' , $mxDateTime );
}
else
{
if( ! is_string( $mxDateTime ) )
{
$mxDateTime = date( 'Y-m-d H:i:s' );
}
}
$objCurrentTime = new DateTime();
$objTimeDifference = $objCurrentTime->diff( new DateTime( $mxDateTime ) );
$objTimeDifference->w = floor( $objTimeDifference->d / 7 );
$objTimeDifference->d -= $objTimeDifference->w * 7;
if( ( is_bool( $bReturnAsArray ) ) && ( $bReturnAsArray ) )
{
return array(
'year' => $objTimeDifference->y , 'y' => $objTimeDifference->y ,
'month' => $objTimeDifference->m , 'm' => $objTimeDifference->m ,
'week' => $objTimeDifference->w , 'w' => $objTimeDifference->w ,
'day' => $objTimeDifference->d , 'd' => $objTimeDifference->d ,
'hour' => $objTimeDifference->h , 'h' => $objTimeDifference->h ,
'minute' => $objTimeDifference->i , 'i' => $objTimeDifference->i ,
'second' => $objTimeDifference->s , 's' => $objTimeDifference->s );
}
if( ! is_array( $arrLabelSet ) )
{
$arrLabelSet = array( 'y' => 'year' ,
'm' => 'month' ,
'w' => 'week' ,
'd' => 'day' ,
'h' => 'hour' ,
'i' => 'minute' ,
's' => 'second' ,
'ps' => 's' ,
'px' => 'just now' );
}
foreach( $arrLabelSet as $strKey => &$mxValue )
{
if( strlen( $strKey ) > 1 )
{
continue;
}
if( $objTimeDifference->$strKey )
{
$mxValue = $objTimeDifference->$strKey . ' ' . $mxValue .
( $objTimeDifference->$strKey > 1 ?
$arrLabelSet[ 'ps' ] :
'' );
}
else
{
$arrLabelSet[ $strKey ] = null;
unset( $arrLabelSet[ $strKey ] );
}
}
$strPXTmp = $arrLabelSet[ 'px' ];
$arrLabelSet[ 'px' ] = null;
unset( $arrLabelSet[ 'px' ] );
$arrLabelSet[ 'ps' ] = null;
unset( $arrLabelSet[ 'ps' ] );
return ( $arrLabelSet ) ?
implode( ', ' , $arrLabelSet ) :
$strPXTmp;
}
?>