This article mainly introduces PHP to calculate a given time before the use of the function, the example analysis of PHP computing time skills, very practical value, the need for friends can refer to the next
The example in this article describes the function usage before PHP calculates a given time. Specific as follows:
Given a time here, calculate how long before this time, for example: 2 days ago, 1 years ago
<?phpfunction prettydate ($date) {$time = Strtotime ($date); $now = time (); $ago = $now-$time; if ($ago <) {$when = round ($ago); $s = ($when = = 1)? " Second ":" Seconds "; return "$when $s ago"; }elseif ($ago < 3600) {$when = round ($ago/60); $m = ($when = = 1)? " Minute ":" Minutes "; return "$when $m ago"; }elseif ($ago >= 3600 && $ago < 86400) {$when = round ($ago/60/60); $h = ($when = = 1)? " Hour ":" Hours "; return "$when $h ago"; }elseif ($ago >= 86400 && $ago < 2629743.83) {$when = round ($ago/60/60/24); $d = ($when = = 1)? " Day ': "Days"; return "$when $d ago"; }elseif ($ago >= 2629743.83 && $ago < 31556926) {$when = round ($ago/60/60/24/30.4375); $m = ($when = = 1)? " Month ":" Months "; return "$when $m ago"; }else{$when = round ($ago/60/60/24/365); $y = ($when = = 1)? " Year ":" Years "; return "$when $y ago"; }}echo prettydate ("2012-07-22 12:23:45"). " <br/> "; Echo pRettydate ("2010-11-12 22:25:45"). " <br/> "; Echo prettydate (" 2012-01-01 01:00:00 ")." <br/> "; Echo prettydate (" 2001-05-30 00:00:00 ")." <br/> ";
Summary : The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Php+mysql+jquery implementation of password retrieval function
PHP implementation loading fonts and saving
PHP recursive traversal for unlimited classification