PHP timestamp function. 1. the PHP timestamp function converts a date to a unix Timestamp. The PHP code is as follows echo :. strtotime (2012-12-21) 2. convert the timestamp to system Time 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; } |
Timestamp PHP code: echo timestamp:. strtotime () 2. convert the timestamp to system time...