Datetime-PHP semantic time idate

Source: Internet
Author: User
Tags idate
I stripped a semantic time code from typecho and modified it as needed. I found a problem during the test: (assume that the Unix timestamp $ form differs by 300 from $ now, they are in different small time periods (idate (H), so that the operations 5 minutes ago are shown as 1 hour ago, but there is no... I stripped a semantic time code from typecho and made modifications as needed. I found a problem during the test :(
Assume that the Unix timestamp $ form differs by 300 from $ now, and they are in different time periods (idate (H), so that the operations 5 minutes ago are displayed as one hour ago, however, this phenomenon is not found in TE ^ -!,
The problem is that $ between <3600 and idate (H) differ by 1. How can I fix this problem? I am a PHP layman, and I am ashamed to ask!

If ($ between <3600 & idate ('h', $ from) = idate ('h', $ now )){

Function dateWord ($ from) {$ now = time (); $ between = $ now-$ from; $ s = date ('y, m, D, H: I ', $ from); if ($ between> 0 & $ between <86400 & idate ('D', $ from) = idate ('D', $ now )) {if ($ between <3600 & idate ('h', $ from) = idate ('h', $ now )) {if ($ between <60 & idate ('I', $ from) = idate ('I', $ now) {$ second = idate ('s ', $ now)-idate ('s ', $ from); if (0 = $ second) {return' ';} else {return ''. $ second. 'seconds ago' ;}}$ min = idate ('I', $ now)-idate ('I', $ from); return ''. $ min. 'minute ago ';} $ hour = idate ('h', $ now)-idate ('h', $ from); return ''. $ hour. 'hour ago ';}

}

The source code is as follows:

/*** Semantic conversion time ** @ access public * @ param string $ from start time * @ param string $ now end time * @ return string */public static function dateWord ($ from, $ now) {$ between = $ now-$ from; /** if it is a day */if ($ between> 0 & $ between <86400 & idate ('D', $ from) = idate ('D ', $ now) {/** if it is an hour */if ($ between <3600 & idate ('h', $ from) = idate ('h ', $ now) {/** for a minute */if ($ between <60 & idate ('I', $ from) = idate ('I ', $ now) {$ second = idate ('s', $ now)-idate ('s', $ from); if (0 = $ second) {return _ t ('');} else {return sprintf (_ n ('% d seconds ago', '% d seconds ago', $ second ), $ second) ;}$ min = idate ('I', $ now)-idate ('I', $ from ); return sprintf (_ n ('% d minutes ago', '% d minutes ago', $ min), $ min);} $ hour = idate ('h ', $ now)-idate ('h', $ from); return sprintf (_ n ('% d hour before', '% d hour before', $ hour ), $ hour);}/** if it is yesterday */if ($ between> 0 & $ between <172800 & (idate ('Z', $ from) + 1 = idate ('Z', $ now) | idate ('Z', $ from)> 2 + idate ('Z', $ now ))) {return _ t ('Yesterday % s', date ('H: I ', $ from ));} /** for a week */if ($ between> 0 & $ between <604800 & idate ('w', $ from) = idate ('w ', $ now) {$ day = intval ($ between/(3600*24); return sprintf (_ n ('% d days before', '% d days before ', $ day), $ day);}/** if it is */if ($ between> 0 & $ between <31622400 & idate ('y', $ from) = idate ('y', $ now) {return date (_ t ('n month j '), $ from );} return date (_ t ('y, m, dday'), $ from );}

Reply content:

I stripped a semantic time code from typecho and made modifications as needed. I found a problem during the test :(
Assume that the Unix timestamp $ form differs by 300 from $ now, and they are in different time periods (idate (H), so that the operations 5 minutes ago are displayed as one hour ago, however, this phenomenon is not found in TE ^ -!,
The problem is that $ between <3600 and idate (H) differ by 1. How can I fix this problem? I am a PHP layman, and I am ashamed to ask!

If ($ between <3600 & idate ('h', $ from) = idate ('h', $ now )){

Function dateWord ($ from) {$ now = time (); $ between = $ now-$ from; $ s = date ('y, m, D, H: I ', $ from); if ($ between> 0 & $ between <86400 & idate ('D', $ from) = idate ('D', $ now )) {if ($ between <3600 & idate ('h', $ from) = idate ('h', $ now )) {if ($ between <60 & idate ('I', $ from) = idate ('I', $ now) {$ second = idate ('s ', $ now)-idate ('s ', $ from); if (0 = $ second) {return' ';} else {return ''. $ second. 'seconds ago' ;}}$ min = idate ('I', $ now)-idate ('I', $ from); return ''. $ min. 'minute ago ';} $ hour = idate ('h', $ now)-idate ('h', $ from); return ''. $ hour. 'hour ago ';}

}

The source code is as follows:

/*** Semantic conversion time ** @ access public * @ param string $ from start time * @ param string $ now end time * @ return string */public static function dateWord ($ from, $ now) {$ between = $ now-$ from; /** if it is a day */if ($ between> 0 & $ between <86400 & idate ('D', $ from) = idate ('D ', $ now) {/** if it is an hour */if ($ between <3600 & idate ('h', $ from) = idate ('h ', $ now) {/** for a minute */if ($ between <60 & idate ('I', $ from) = idate ('I ', $ now) {$ second = idate ('s', $ now)-idate ('s', $ from); if (0 = $ second) {return _ t ('');} else {return sprintf (_ n ('% d seconds ago', '% d seconds ago', $ second ), $ second) ;}$ min = idate ('I', $ now)-idate ('I', $ from ); return sprintf (_ n ('% d minutes ago', '% d minutes ago', $ min), $ min);} $ hour = idate ('h ', $ now)-idate ('h', $ from); return sprintf (_ n ('% d hour before', '% d hour before', $ hour ), $ hour);}/** if it is yesterday */if ($ between> 0 & $ between <172800 & (idate ('Z', $ from) + 1 = idate ('Z', $ now) | idate ('Z', $ from)> 2 + idate ('Z', $ now ))) {return _ t ('Yesterday % s', date ('H: I ', $ from ));} /** for a week */if ($ between> 0 & $ between <604800 & idate ('w', $ from) = idate ('w ', $ now) {$ day = intval ($ between/(3600*24); return sprintf (_ n ('% d days before', '% d days before ', $ day), $ day);}/** if it is */if ($ between> 0 & $ between <31622400 & idate ('y', $ from) = idate ('y', $ now) {return date (_ t ('n month j '), $ from );} return date (_ t ('y, m, dday'), $ from );}

Because the code you stripped out only judges the situation of the same hour, and does not judge the situation of the adjacent hour but the difference of 60 minutes, so I addedgapHourIs used to represent a small time difference, when the time difference is 60 minutes, makeifJudge, if it is processed by default in the same hour($gapHour == 0), A time difference($gapHour == 1).

Function dateWord ($ from) {$ now = time (); $ between = $ now-$ from; $ s = date ('y, m, D, H: I ', $ from); $ fromHour = idate ('h', $ from); $ nowHour = idate ('h', $ now); $ gapHour = $ nowHour-$ fromHour; if ($ between> 0 & $ between <86400 & idate ('D', $ from) = idate ('D', $ now )) {if ($ between <3600) {if ($ gapHour = 0) {if ($ between <60 & idate ('I', $ from) = idate ('I', $ now) {$ second = idate ('s', $ now)-idate ('s', $ from ); if (0 = $ second) {return ';} else {return ''. $ second. 'seconds ago' ;}}$ min = idate ('I', $ now)-idate ('I', $ from); return ''. $ min. 'minute ago ';} else if ($ gapHour = 1) {if ($ between <60 & idate (' I ', $ from) = idate ('I', $ now) {$ second = idate ('s', $ now) + (60-idate ('s', $ from )); if (0 = $ second) {return ';} else {return ''. $ second. 'seconds ago' ;}}$ min = idate ('I', $ now) + (60-idate ('I', $ from); return ''. $ min. 'minute ago '; }}$ hour = idate ('h', $ now)-idate ('h', $ from); return ''. $ hour. 'hour ago ';}}

A large piece of code looks dizzy, and it is not very recommended by the official team now.date()The function is used to operate the time object, so I have rewritten the entire code:

Function dateWord ($ from, $ now) {$ timezone = new DateTimeZone ('Asia/Shanghai'); $ now = new DateTime ($ now, $ timezone ); $ from = new DateTime ($ from, $ timezone); $ between = $ now-> diff ($ from); if (! $ Between-> invert) return false;/** if it exceeds one year **/if ($ between-> y) return $ from-> format ('y, m, dday');/** more than seven days in a year **/if ($ between-> days> 6) return $ from-> format ('n month j ');/** within a week but greater than two days **/if ($ between-> days> 1) return $ between-> format ('% d days ago');/** if it was yesterday **/if ($ between-> days) return $ from-> format ('Yesterday H: I ');/** if more than one hour in a day **/if ($ between-> h> 1) return $ between-> format ('% h ago'); if ($ between-> I> 1) r Eturn $ between-> format ('% I minutes ago'); return $ between-> s? $ Between-> format ('% s before'): '';} function dateWordToHtml ($ from, $ now = 'right ') {$ dateWord = dateWord ($ from, $ now); $ from = new DateTime ($ from, new DateTimeZone ('Asia/Shanghai ')); $ fromWord = $ from-> format ('y, m, D, H: I '); return "$ dateWord";} echo dateWordToHtml ");

dateWord()The function corresponds to the original Typecho return,dateWordToHtml()The function corresponds to the function you modified (another function is created to facilitate other people's reference ).

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.