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 ');