PHP timestamp function (several minutes, several hours ago, several days ago, etc)

Source: Internet
Author: User
Tags current time echo date

1. The PHP timestamp function converts a date to a unix timestamp.

Doomsday timestamp PHP

The code is as follows: Copy code

Echo "doomsday timestamp:". strtotime ("2012-12-21 ")


2. Convert the timestamp to the system time

The code is as follows: Copy code


Date ('Y-m-d H: I: S', "1228348800 ");

3. The PHP timestamp function obtains the date and time of English text as follows:

For ease of comparison, date is used to convert a timestamp from a specified timestamp to a system time

(1) print the timestamp strtotime ("+ 1 day") at this time tomorrow ")

The code is as follows: Copy code

// Current time
Echo date ("Y-m-d H: I: s", time ());
// Tomorrow at this time
Echo date ("Y-m-d H: I: s", strtotime ("+ 1 day "));


(2) print the timestamp strtotime ("-1 day") at this time yesterday ")

The code is as follows: Copy code

// Current time
Echo date ("Y-m-d H: I: s", time ());
// Specify the time
Echo date ("Y-m-d H: I: s", strtotime ("-1 day "));


(3) print the timestamp strtotime ("+ 1 week") for the next week ")

The code is as follows: Copy code


// Current time
Echo date ("Y-m-d H: I: s", time ());
// Next week
Echo date ("Y-m-d H: I: s", strtotime ("+ 1 week "));


(4) print the timestamp strtotime ("-1 week") of the last week ")

The code is as follows: Copy code

// Current time
Echo date ("Y-m-d H: I: s", time ());
// Time of the last week
Echo date ("Y-m-d H: I: s", strtotime ("-1 week "));


(5) print the specified timestamp strtotime for the next week ("next Thursday ")

 

The code is as follows: Copy code
// Current time
Echo date ("Y-m-d H: I: s", time ());
// The time of the next week
Echo date ("Y-m-d H: I: s", strtotime ("next Thursday "));


(6) print the timestamp strtotime ("last Thursday") of the specified last week ")

The code is as follows: Copy code

// Current time
Echo date ("Y-m-d H: I: s", time ());
// Specify the time
Echo date ("Y-m-d H: I: s", strtotime ("last Thursday "));


The above PHP timestamp function example shows that strtotime can parse the date and time descriptions of any English text into Unix timestamps. We use mktime () or date () format the date and time to get the specified timestamp.

In other words, I tested such a function written by someone else. Some minor problems are: for cross-year dates, the year is not displayed. Modify as follows:

The code is as follows: Copy code

Function mdate ($ time = NULL ){
$ Text = '';
$ Time = NULL | $ time> time ()? Time (): intval ($ time );
$ T = time ()-$ time; // time difference (seconds)
$ Y = date ('Y', $ time)-date ('Y', time (); // whether to cross-year
Switch ($ t ){
Case $ t = 0:
$ Text = 'hangzhou ';
Break;
Case $ t <60:
$ Text = $ t. 'Seconds ago '; // within one minute
Break;
Case $ t <60*60:
$ Text = floor ($ t/60). 'Minute ago '; // within one hour
Break;
Case $ t <60*60*24:
$ Text = floor ($ t/(60*60). 'Hour before '; // within a day
Break;
Case $ t <60*60*24*3:
$ Text = floor ($ time/(60*60*24) = 1? 'Yesterday '. date ('H: I', $ time): 'Day before yesterday '. date ('H: I', $ time); // yesterday and the day before yesterday
Break;
Case $ t <60*60*24*30:
$ Text = date ('m Month D day H: I ', $ time); // within a month
Break;
Case $ t <60*60x24*365 & $ y = 0:
$ Text = date ('mmonth dday', $ time); // within one year
Break;
Default:
$ Text = date ('y, m, dday', $ time); // one year ago
Break;
    }
       
Return $ text;
}

In ThinkPHP:

Put the written functions in the Common folder. The system automatically loads the data.
Put it on this common. php page. The common. php format is inherent and should not be changed.

Direct calls in the template

The code is as follows: Copy code

{$ Vo. time | mdate}

Example 2: Simple

The code is as follows: Copy code

Function formatDate ($ sTime ){
// STime = source time, cTime = current time, dTime = time difference
$ CTime = time ();
$ DTime = $ cTime-$ sTime;
$ DDay = intval (date ("Ymd", $ cTime)-intval (date ("Ymd", $ sTime ));
$ DYear = intval (date ("Y", $ cTime)-intval (date ("Y", $ sTime ));
If ($ dTime <60 ){
$ DTime = $ dTime. "seconds ago ";
} Elseif ($ dTime <3600 ){
$ DTime = intval ($ dTime/60). "minutes ago ";
} Elseif ($ dTime >=3600 & $ dDay = 0 ){
$ DTime = "today". date ("H: I", $ sTime );
} Elseif ($ dYear = 0 ){
$ DTime = date ("m-d H: I", $ sTime );
} Else {
$ DTime = date ("Y-m-d H: I", $ sTime );
 }
Return $ dTime;
}

 

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.