This article mainly shares with you the detailed explanation of the PHP timestamp function, mainly in the form of code. I hope it can help you.
echo "The doomsday timestamp is:".strtotime("2012-12-21")
2. Convert the timestamp to system time
date('Y-m-d H:i:s',"1228348800");
(1) Get the zero o'clock timestamp of the current day
$timetoday = strtotime(date("Y-m-d",time()));
(2) Get the zero o'clock timestamp of tomorrow
$tomorrow = $timetoday + 3600*24;
3. The example of the PHP timestamp function to obtain the English text date and time is as follows:
For comparison, use date to convert the current timestamp and the specified timestamp into system time
(1) Print the timestamp at this time tomorrow strtotime(”+1 day”)
//当前时间 echo date("Y-m-d H:i:s",time()); //明天此时时间 echo date("Y-m-d H:i:s",strtotime("+1 day"));
(2) Print the timestamp at this time yesterday strtotime("-1 day")
//当前时间 echo date("Y-m-d H:i:s",time()) ; //指定时间 echo date("Y-m-d H:i:s",strtotime("-1 day"));
(3) Print the timestamp at this time next week strtotime("+1 week")
//当前时间 echo date("Y-m-d H:i:s",time()); //下星期时间 echo date("Y-m-d H:i:s",strtotime("+1 week"));
(4) Print the timestamp of last week at this time strtotime("-1 week")
//当前时间 echo date("Y-m-d H:i:s",time()); //上个星期此时时间 echo date("Y-m-d H:i:s",strtotime("-1 week"));
(5) Print the timestamp of the specified day of the next week strtotime("next Thursday")
//当前时间 echo date("Y-m-d H:i:s",time()); //下星期几时间 echo date("Y-m-d H:i:s",strtotime("next Thursday"));
(6) Print the timestamp of the specified day of the week strtotime("last Thursday")
//当前时间 echo date("Y-m-d H:i:s",time()); //指定时间 echo date("Y-m-d H:i:s",strtotime("last Thursday"));
As can be seen from the above PHP timestamp function example, strtotime can parse the date and time description of any English text For Unix timestamps, we use mktime() or date() to format the date and time to obtain the specified timestamp and achieve the required date and time.
I saw such a function written by others and tested it. Some minor glitches: For New Year's Eve dates, the year is not displayed. Modify as follows
function mdate($time = NULL) { $text = ''; $time = $time === NULL || $time > time() ? time() : intval($time); $t = time() - $time; //时间差 (秒) $y = date('Y', $time)-date('Y', time());//是否跨年 switch($t){ case $t == 0: $text = '刚刚'; break; case $t < 60: $text = $t . '秒前'; // 一分钟内 break; case $t < 60 * 60: $text = floor($t / 60) . '分钟前'; //一小时内 break; case $t < 60 * 60 * 24: $text = floor($t / (60 * 60)) . '小时前'; // 一天内 break; case $t < 60 * 60 * 24 * 3: $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天 break; case $t < 60 * 60 * 24 * 30: $text = date('m月d日 H:i', $time); //一个月内 break; case $t < 60 * 60 * 24 * 365&&$y==0: $text = date('m月d日', $time); //一年内 break; default: $text = date('Y年m月d日', $time); //一年以前 break; } return $text; }
ThinkPHP:
Put the written function in the Common folder. The system will load it automatically.
Put it in this common.php page, common.php has its own format, do not change the name.
Call directly in the template
{$vo.time|mdate}
Example 2, a simpler one
function formatDate($sTime) { //sTime=源时间,cTime=当前时间,dTime=时间差 $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."秒前"; }elseif( $dTime < 3600 ){ $dTime = intval($dTime/60)."分钟前"; }elseif( $dTime >= 3600 && $dDay == 0 ){ $dTime = "今天".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; }
Here is a time-friendly method for everyone
/** * 友好时间显示 * @param $time * @return bool|string */ function friend_date($time) { if (!$time) return false; $fdate = ''; $d = time() - intval($time); $ld = $time - mktime(0, 0, 0, 0, 0, date('Y')); //得出年 $md = $time - mktime(0, 0, 0, date('m'), 0, date('Y')); //得出月 $byd = $time - mktime(0, 0, 0, date('m'), date('d') - 2, date('Y')); //前天 $yd = $time - mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); //昨天 $dd = $time - mktime(0, 0, 0, date('m'), date('d'), date('Y')); //今天 $td = $time - mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')); //明天 $atd = $time - mktime(0, 0, 0, date('m'), date('d') + 2, date('Y')); //后天 if ($d == 0) { $fdate = '刚刚'; } else { switch ($d) { case $d < $atd: $fdate = date('Y年m月d日', $time); break; case $d < $td: $fdate = '后天' . date('H:i', $time); break; case $d < 0: $fdate = '明天' . date('H:i', $time); break; case $d < 60: $fdate = $d . '秒前'; break; case $d < 3600: $fdate = floor($d / 60) . '分钟前'; break; case $d < $dd: $fdate = floor($d / 3600) . '小时前'; break; case $d < $yd: $fdate = '昨天' . date('H:i', $time); break; case $d < $byd: $fdate = '前天' . date('H:i', $time); break; case $d < $md: $fdate = date('m月d日 H:i', $time); break; case $d < $ld: $fdate = date('m月d日', $time); break; default: $fdate = date('Y年m月d日', $time); break; } } return $fdate; }
Related recommendations:
Detailed explanation of PHP's function to obtain the current timestamp
The above is the detailed content of Detailed explanation of PHP timestamp function. For more information, please follow other related articles on the PHP Chinese website!