Blog Image

Calculate number of days,hours, minute, seconds passed from todays date to some past date

<?php
function get_time_differenceFromToday($fromDate)
{
$uts[‘start’] = strtotime( $fromDate );//date format YYYY-MM-DD HH:MM:SS
$uts[‘end’] = time();//unix timestamp //strtotime( $end )
if( $uts[‘start’]!==-1 && $uts[‘end’]!==-1 )
{
if( $uts[‘end’] >= $uts[‘start’] )
{
$diff = $uts[‘end’] – $uts[‘start’];

if($diff > 604800)
{
return date(“m-d-Y H:i”,strtotime($fromDate));
}
else
{
if( $weeks=intval((floor($diff/604800))) )
$diff = $diff % 604800;
if( $days=intval((floor($diff/86400))) )
$diff = $diff % 86400;
if( $hours=intval((floor($diff/3600))) )
$diff = $diff % 3600;
if( $minutes=intval((floor($diff/60))) )
$diff = $diff % 60;
$diff = intval( $diff );
if($weeks==0)
{
$weeks = “”;
}
else
{
$weeks = $weeks.” week “;
}
if ($days == 0)
{
$days = “”;
}
else
{
$days = $days.” days “;
}
if($hours == 0)
{
$hours = “”;
}
else
{
$hours = $hours.” hours “;
}
if ($minutes == 0)
{
$minutes = “”;
}
else
{
$minutes = $minutes.” minute “;
}
if ($diff == 0)
{
$second = “”;
}
else
{
$second = $diff.” second “;
}
return ” “.$weeks.$days.$hours.$minutes.$second.”ago”;
//return( array(‘weeks’=>$weeks,’days’=>$days, ‘hours’=>$hours, ‘minutes’=>$minutes, ‘seconds’=>$diff) );
}

}
else
{
return “Ending date/time is earlier than the start date/time”;
}
}
else
{
return “Invalid date/time data detected”;
}
return( false );
}



Author: admin

Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management. He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.