There are many ways to express time and date in PHP, the most common is the time stamp and the normal date format, let me introduce the time stamp and the date conversion.
1.php Time conversion function
Strtotime
The strtotime () function resolves the datetime description of any English text to a Unix timestamp.
Grammar
Strtotime (Time,now)
Cases
The code is as follows |
Copy Code |
Strtotime ("Today") |
Date
The PHP date () function formats the timestamp as a date and time that is more readable.
Grammar
Date (Format,timestamp)
Cases
The code is as follows |
Copy Code |
echo Date ("y/m/d"); echo " "; echo Date ("Y.M.D"); echo " "; echo Date ("y-m-d"); ?> |
Convert Timestamp to date
The code is as follows |
Copy Code |
Date ("Y-m-d h:i", $unixtime) |
Get a timestamp of today's 0 points in 2.php
To get a 0-point Unix timestamp, you can use $todaytime=strtotime ("Today"),
Then use Date ("Y-m-d h:i", $todaytime) to convert to a date.
3.php timestamps are converted to dates and display different content by 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"); $time 1 = time ()-$todaytime; if ($time < 60) { $str = ' just '; }else if ($time < 60 * 60) { $min = Floor ($time/60); $str = $min. ' Minutes ago '; }else if ($time < $time 1) { $str = ' Today '. $htime; }else{ $str = $rtime; } return $str; } |
In this function you can add more comparisons to make the displayed date more specific, such as adding seconds before the day before, and so on more specific dates.
4.php date in 0 and do not fill 0
echo Date (' y-m-d '); Show 2012-08-08
echo Date (' y-n-j '); Show 2012-8-8
http://www.bkjia.com/PHPjc/628942.html www.bkjia.com true http://www.bkjia.com/PHPjc/628942.html techarticle There are many ways to express time and date in PHP, the most common is the time stamp and the normal date format, let me introduce the time stamp and the date conversion. 1.php Time Conversion ...