Created
May 31, 2011 05:45
-
-
Save ddliu/1000031 to your computer and use it in GitHub Desktop.
get date difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Great work guy!