Php computing several minutes ago, several hours ago, several days ago, several functions and classes sharing _ PHP Tutorial

Source: Internet
Author: User
Php computing shares several functions and classes several minutes ago, several hours ago, and a few days ago. I. function implementation example 1: Copy the code as follows: functiontime_tran ($ the_time) {$ now_timedate (Y-m-dH: I: s, time () + 8*60*60); $ now_timestrtotime ($ now_time); $ show_time 1. 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 ');
*
*/

Worker instance 1: 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...

Related Article

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.