"How long is it before" in PHP

Source: Internet
Author: User
It is often seen that some video stations show a video, how many days ago, how many minutes ago, and how many years ago,
In fact, it is easy to implement. The following is a function:

<? PHP
Include_once ('timeago. php ');
$ Cur_time1 = "1291684422"; // UNIX time, unified use
Echo time_ago ($ cur_time1 );
?>
Timeago. php
<? PHP
Function time_ago ($ cur_time ){
$ Time _ = Time ()-$ cur_time;

$ Seconds = $ time _;
$ Minutes = round ($ time _/60 );
$ Hours = round ($ time _/ 3600 );
$ Days = round ($ time _/86400 );
$ Weeks = round ($ time _/ 604800 );
$ Months = round ($ time _/ 2419200 );
$ Years = round ($ time _/ 29030400 );

// Seconds
If ($ seconds <= 60 ){

$ Time = "$ seconds ago ";

// Minutes
} Else if ($ minutes <= 60 ){

If ($ minutes = 1 ){
$ Time = "one minute ago ";
} Else {
$ Time = "$ minutes Minutes Ago ";
}

// Hours
} Else if ($ hours <= 24 ){

If ($ hours = 1 ){
$ Time = "one hour ago ";
} Else {
$ Time = "$ hours ago ";
}

// Days
} Else if ($ days <= 7 ){

If ($ days = 1 ){
$ Time = "one day ago ";
} Else {
$ Time = "$ Days Ago ";
}

// Weeks
} Else if ($ weeks <= 4 ){

If ($ weeks = 1 ){
$ Time = "one week ago ";
} Else {
$ Time = "$ weeks Weeks Ago ";
}

// Months
} Else if ($ months <= 12 ){

If ($ months = 1 ){
$ Time = "one month ago ";
} Else {
$ Time = "$ months ago ";
}

// Years
} Else {

If ($ year = 1 ){
$ Time = "one year ago ";
} Else {
$ Time = "$ year Years Ago ";
}

}
Return $ time;
}
?>

Note: Due to the English and Chinese grammar habits, in Chinese, there is no plural representation on the first and second years, so the aboveCodeYou can use it by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.