In php, there are many expressions of time and date. The most common expressions are timestamps and common date formats. Next I will introduce the conversion between timestamps and dates.
1. Time Conversion Function in php
Strtotime
The strtotime () function parses the date and time descriptions of any English text into Unix timestamps.
Syntax
Strtotime (time, now)
Example
The Code is as follows: |
Copy code |
Strtotime ("today ") |
Date
The PHP Date () function can format the timestamp as a Date and time with better readability.
Syntax
Date (format, timestamp)
Example
The Code is as follows: |
Copy code |
<? Php Echo date ("Y/m/d "); Echo "<br/> "; Echo date ("Y. m. d "); Echo "<br/> "; Echo date ("Y-m-d "); ?> |
Convert timestamp to date
The Code is as follows: |
Copy code |
Date ("Y-m-d H: I", $ unixtime) |
2. Obtain the timestamp of today's in php.
To obtain the unix timestamp at zero point, you can use $ todaytime = strtotime ("today "),
Then use date ("Y-m-d H: I", $ todaytime) to convert to date.
3. In php, the timestamp is converted to a date and different content is displayed according to the time, such as just, minutes ago, hours ago, today, yesterday, etc.
/* Time Conversion Function */
The Code is as follows: |
Copy code |
Function transTime ($ ustime ){ $ Ytime = date ("Y-m-d H: I", $ ustime ); $ Rtime = date ("n month j day H: I", $ ustime ); $ Htime = date ("H: I", $ ustime ); $ Time = time ()-$ ustime; $ Todaytime = strtotime ("today "); $ Time1 = time ()-$ todaytime; If ($ time <60 ){ $ Str = 'gang '; } Else if ($ time <60*60 ){ $ Min = floor ($ time/60 ); $ Str = $ min. 'minute ago '; } Else if ($ time <$ time1 ){ $ Str = 'today '. $ htime; } Else { $ Str = $ rtime; } Return $ str; } |
In this function, you can add more comparisons to make the displayed date more specific. For example, you can add more specific dates before the second or the day before yesterday.
4. Fill 0 in php date and do not fill 0
Echo date ('Y-m-d'); Display
Echo date ('Y-n-J'); Display