Home >Backend Development >PHP Tutorial >Conversion program between timestamp and date in PHP_PHP tutorial
There are many ways to express time and date in PHP. The most commonly used ones are timestamp and ordinary date format. Let me introduce the conversion between timestamp and date.
Time conversion function in 1.php
strtotime
Thestrtotime() function parses any English text datetime description into a Unix timestamp.
Grammar
strtotime(time,now)
Example
The code is as follows | Copy code | ||||
|
The PHP Date() function can format the timestamp into a more readable date and time.
Grammar
Example
代码如下 | 复制代码 |
echo date("Y/m/d"); |
The code is as follows | Copy code | ||||
echo date("Y/m/d");
"; echo date("Y.m.d"); echo " "; echo date("Y-m-d"); ?> |
The code is as follows | Copy code |
date("Y-m-d H:i",$unixtime) |
Get today’s zero time timestamp in 2.php
To get 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.
Convert the timestamp in 3.php to date, and display different contents according to time, such as just now, 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"); |
}else if($time < 60 * 60){