Timestamp is a small feature that we often use in comparison of time and date, next, I will introduce some methods for converting the strtotime Timestamp and use it to format a date several minutes, several hours ago, or several days ago. Timestamp is a small feature that we often use in comparison of time and date, next, I will introduce some methods for converting the strtotime Timestamp and use it to format a date several minutes, several hours ago, or several days ago.
Script ec (2); script
1. the PHP timestamp function converts a date to a unix timestamp.
Doomsday timestamp PHP
The Code is as follows: |
|
Echo "Doomsday timestamp:". strtotime ("2012-12-21 ") |
2. Convert the timestamp to the system time
The Code is as follows: |
|
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: |
|
// 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: |
|
// 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: |
|
// 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: |
|
// 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: |
|
// 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: |
|
// 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: |
|
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: |
|
{$ Vo. time | mdate} |
Example 2: simple
The Code is as follows: |
|
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; } |