Php calculates how long ago two functions are sent

Source: Internet
Author: User
Tags echo date

Example,

How long ago are time functions commonly used in forums and blogs?

The code is as follows: Copy code

Function timeFromNow ($ dateline ){
If (empty ($ dateline) return false;
$ Seconds = time ()-$ dateline;
If ($ seconds <60 ){
Return "1 minute ago ";
} Elseif ($ seconds <3600 ){
Return floor ($ seconds/60). "minutes ago ";
} Elseif ($ seconds <24*3600 ){
Return floor ($ seconds/3600). "hours ago ";
} Elseif ($ seconds <48*3600 ){
Return date ("yesterday H: I", $ dateline )."";
} Else {
Return date ('Y-m-D', $ dateline );
    }
}

Echo timeFromNow (strtotime ("14:15:13"); // yesterday
Echo timeFromNow (strtotime ("14:15:13"); // Before 1

Note: The time zone is not taken into account here.

Later, I saw a piece of code on the Internet.

 

The code is as follows: Copy code
Function time_tran ($ the_time ){
$ Now_time = date ("Y-m-d H: I: s", time () + 8*60*60 );
$ Now_time = strtotime ($ now_time );
$ Show_time = strtotime ($ the_time );
$ Dur = $ now_time-$ show_time;
If ($ dur <0 ){
Return $ the_time;
} Else {
If ($ dur <60 ){
Return $ dur. 'Seconds ago ';
} Else {
If ($ dur <3600 ){
Return floor ($ dur/60). 'Minute ago ';
} Else {
If ($ dur <86400 ){
Return floor ($ dur/3600). 'Hour ago ';
} Else {
If ($ dur <259200) {// within 3 days
Return floor ($ dur/86400). 'Days ago ';
} Else {
Return $ the_time;
  }
 }
}

Last time zone added

In php. ini, the default value is date. timezone = UTC. Change to China time zone and date. timezone = PRC. If the GMT format is directly written, it is date. timezone = Etc/GMT + 8.
You can also set it in the PHP page header.
Date_default_timezone_set ('prc ');
Test:
Echo date ('Y-m-d H: I: s ');

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.