Friday, September 12, 2008

Function to get Date time duration for display

function fnGetDateTimeDuration($strActualDateTime)
{
/*
About 1 second ago ≤1 second
A few seconds ago >1 second
About a minute ago ≥30 seconds
[x] minutes ago ≥1 minute, 30 seconds
[full time] >59 minutes
Today >1 hour, 30 minutes
Yesterday >today's date
[full date] >yesterday's date
*/

if(!$strActualDateTime)
{
return ;
}


/*
Date parse:
i/p -> date_parse("2006-12-12 10:00:00.5")
o/p ->
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)

*/


//$strDateDiff = time() - strtotime(str_replace("-","/",$strActualDateTime));


$strDateDiff = time() - strtotime($strActualDateTime);

//print "$strDateDiff = ".time()." - ".strtotime($strActualDateTime)."
";

/*
if($strDateDiff>31536000) // before year
{
//$strTempVal = round($strDateDiff/31536000,0).' year';
$strTempVal = date("j F Y",strtotime($strActualDateTime));
}
elseif($strDateDiff>2419200) // before month
{
//$strTempVal = round($strDateDiff/2419200,0).' month';
$strTempVal = date("j F Y",strtotime($strActualDateTime));
}
elseif($strDateDiff>604800) // before week
{
//$strTempVal = round($strDateDiff/604800,0).' week';
$strTempVal = date("j F Y",strtotime($strActualDateTime));
}
elseif($strDateDiff>259200) // before 2 day
{
//$strTempVal = round($strDateDiff/604800,0).' week';
$strTempVal = date("j F Y",strtotime($strActualDateTime));
}
else*/

if($strDateDiff>172800) // before yesterday
{
$strTempVal = date("j F Y",strtotime($strActualDateTime));
}
elseif($strDateDiff>86400) // before day
{
//$strTempVal = round($strDateDiff/86400,0).' day';
$strTempVal = 'Yesterday';
}
elseif($strDateDiff>3780) // before hr and 30 mins
{
$strTempVal = 'Today';
}
elseif($strDateDiff>3600) // before hr
{
//$strTempVal = round($strDateDiff/3600,0).' hour';
$arrStartArry = date_parse($strActualDateTime);
$strTempVal = $arrStartArry['minute'].".".$arrStartArry['second'];
}
elseif($strDateDiff>=90) // before min and 30 sec
{
$strTempVal = round($strDateDiff/60,0).' minutes ago';
}
elseif($strDateDiff>=30) // before 30 sec
{
$strTempVal = 'About a minute ago';
}
elseif($strDateDiff>1) // before 1 sec
{
$strTempVal = 'A few seconds ago';
}
elseif($strDateDiff<=1) // less then sec
{
$strTempVal = 'About 1 second ago';
}



return $strTempVal;
}

No comments: