Skip to content

Instantly share code, notes, and snippets.

@leoken
Forked from ddliu/date_diff.php
Created February 18, 2013 05:38
Show Gist options
  • Save leoken/4975305 to your computer and use it in GitHub Desktop.
Save leoken/4975305 to your computer and use it in GitHub Desktop.
<?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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment