Home  >  Article  >  Backend Development  >  Detailed explanation of PHP timestamp function

Detailed explanation of PHP timestamp function

小云云
小云云Original
2018-03-29 16:22:362736browse

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 . &#39;秒前&#39;; // 一分钟内
      break;
     case $t < 60 * 60:
      $text = floor($t / 60) . &#39;分钟前&#39;; //一小时内
      break;
     case $t < 60 * 60 * 24:
      $text = floor($t / (60 * 60)) . &#39;小时前&#39;; // 一天内
      break;
     case $t < 60 * 60 * 24 * 3:
      $text = floor($time/(60*60*24)) ==1 ?&#39;昨天 &#39; . date(&#39;H:i&#39;, $time) : &#39;前天 &#39; . date(&#39;H:i&#39;, $time) ; //昨天和前天
      break;
     case $t < 60 * 60 * 24 * 30:
      $text = date(&#39;m月d日 H:i&#39;, $time); //一个月内
      break;
     case $t < 60 * 60 * 24 * 365&&$y==0:
      $text = date(&#39;m月d日&#39;, $time); //一年内
      break;
     default:
      $text = date(&#39;Y年m月d日&#39;, $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 = &#39;&#39;;
    $d = time() - intval($time);
    $ld = $time - mktime(0, 0, 0, 0, 0, date(&#39;Y&#39;)); //得出年
    $md = $time - mktime(0, 0, 0, date(&#39;m&#39;), 0, date(&#39;Y&#39;)); //得出月
    $byd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 2, date(&#39;Y&#39;)); //前天
    $yd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 1, date(&#39;Y&#39;)); //昨天
    $dd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;)); //今天
    $td = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 1, date(&#39;Y&#39;)); //明天
    $atd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 2, date(&#39;Y&#39;)); //后天
    if ($d == 0) {
        $fdate = &#39;刚刚&#39;;
    } else {
        switch ($d) {
            case $d < $atd:
                $fdate = date(&#39;Y年m月d日&#39;, $time);
                break;
            case $d < $td:
                $fdate = &#39;后天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < 0:
                $fdate = &#39;明天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < 60:
                $fdate = $d . &#39;秒前&#39;;
                break;
            case $d < 3600:
                $fdate = floor($d / 60) . &#39;分钟前&#39;;
                break;
            case $d < $dd:
                $fdate = floor($d / 3600) . &#39;小时前&#39;;
                break;
            case $d < $yd:
                $fdate = &#39;昨天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < $byd:
                $fdate = &#39;前天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < $md:
                $fdate = date(&#39;m月d日 H:i&#39;, $time);
                break;
            case $d < $ld:
                $fdate = date(&#39;m月d日&#39;, $time);
                break;
            default:
                $fdate = date(&#39;Y年m月d日&#39;, $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn