This article mainly introduces several functions and classes of php computing several minutes ago, several hours ago, and a few days ago. For more information, see
This article mainly introduces several functions and classes of php computing several minutes ago, several hours ago, and a few days ago. For more information, see
I. function implementation
Instance 1:
The Code is as follows:
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;
}
}
}
Instance 2:
The Code is as follows:
Function format_date ($ time ){
$ T = time ()-$ time;
$ F = array (
'20140901' => 'Year ',
'000000' => 'month ',
'20140901' => 'Week ',
'20140901' => 'day ',
'000000' => 'hourly ',
'60' => 'mine ',
'1' => 'second'
);
Foreach ($ f as $ k => $ v ){
If (0! = $ C = floor ($ t/(int) $ k )){
Return $ c. $ v. 'pre ';
}
}
}
?>
Instance 3:
The Code is as follows:
Function formatTime ($ date ){
$ Str = '';
$ Timer = strtotime ($ date );
$ Diff = $ _ SERVER ['request _ time']-$ timer;
$ Day = floor ($ diff/86400 );
$ Free = $ diff % 86400;
If ($ day> 0 ){
Return $ day. "Days Ago ";
} Else {
If ($ free> 0 ){
$ Hour = floor ($ free/3600 );
$ Free = $ free % 3600;
If ($ hour> 0 ){
Return $ hour. "hours ago ";
} Else {
If ($ free> 0 ){
$ Min = floor ($ free/60 );
$ Free = $ free % 60;
If ($ min> 0 ){
Return $ min. "Minutes Ago ";
} Else {
If ($ free> 0 ){
Return $ free. "seconds ago ";
} Else {
Return 'gang ';
}
}
} Else {
Return 'gang ';
}
}
} Else {
Return 'gang ';
}
}
}
Instance 4:
The Code is as follows:
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;
}
}
}
}
}
}
II. Implementation of Classes
The Code is as follows:
/*
* Author: Solon Ring
* Time: 2011-11-02
* Sending time calculation (year, month, day, hour, minute, second)
* $ Createtime can be the current time
* $ Gettime the time you want to upload
*/
Class Mygettime {
Function _ construct ($ createtime, $ gettime ){
$ This-> createtime = $ createtime;
$ This-> gettime = $ gettime;
}
Function getSeconds ()
{
Return $ this-> createtime-$ this-> gettime;
}
Function getMinutes ()
{
Return ($ this-> createtime-$ this-> gettime)/(60 );
}
Function getHours ()
{
Return ($ this-> createtime-$ this-> gettime)/(60*60 );
}
Function getDay ()
{
Return ($ this-> createtime-$ this-> gettime)/(60*60*24 );
}
Function getMonth ()
{
Return ($ this-> createtime-$ this-> gettime)/(60*60*24*30 );
}
Function getYear ()
{
Return ($ this-> createtime-$ this-> gettime)/(60*60*24*30*12 );
}
Function index ()
{
If ($ this-> getYear ()> 1)
{
If ($ this-> getYear ()> 2)
{
Return date ("Y-m-d", $ this-> gettime );
Exit ();
}
Return intval ($ this-> getYear (). "years ago ";
Exit ();
}
If ($ this-> getMonth ()> 1)
{
Return intval ($ this-> getMonth (). "Months Ago ";
Exit ();
}
If ($ this-> getDay ()> 1)
{
Return intval ($ this-> getDay (). "Days Ago ";
Exit ();
}
If ($ this-> getHours ()> 1)
{
Return intval ($ this-> getHours (). "Hours Ago ";
Exit ();
}
If ($ this-> getMinutes ()> 1)
{
Return intval ($ this-> getMinutes (). "Minutes Ago ";
Exit ();
}
If ($ this-> getSeconds ()> 1)
{
Return intval ($ this-> getSeconds ()-1). "seconds ago ";
Exit ();
}
}
}
// Use instance of the class
/*
*
* Call class Output Method
*
* $ A = new Mygettime (time (), strtotime ('-25 month '));
* Echo iconv ('utf-8', 'gb2312 ', $ a-> index ())? Iconv ('utf-8', 'gb2312', $ a-> index (): iconv ('utf-8', 'gb2312', 'current ');
*
*/