Skip to content

Instantly share code, notes, and snippets.

@ddliu
Created May 31, 2011 05:45
Show Gist options
  • Save ddliu/1000031 to your computer and use it in GitHub Desktop.
Save ddliu/1000031 to your computer and use it in GitHub Desktop.
get date difference
<?php
function getDateDiff($date)
{
$diff_info=array();
$timestamp=strtotime($date);
$timestamp_diff=$timestamp-time();
//test if $date is future
$diff_info['future']=$timestamp_diff>0;
$timestamp_diff=abs($timestamp_diff);
//calculate total date difference
$date_diff=round($timestamp_diff/(24*3600));
//year
$diff_info['year']=floor($date_diff/365);
$date_diff-=$diff_info['year']*365;
//month
$diff_info['month']=floor($date_diff/30.5);
$date_diff-=floor($diff_info['month']*30.5);
//day
$diff_info['day']=$date_diff;
return $diff_info;
}
<?php
$diff=getDateDiff('8/12/2012');
if($diff['future'])
{
echo "You have {$diff['year']} year(s), {$diff['month']} month(s), {$diff['day']} day(s) left";
}
else
{
echo "Your account has expired";
}
@lynnocheu
Copy link

Great work guy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment