Home > PHP Framework > ThinkPHP > How to convert timestamp in thinkphp

How to convert timestamp in thinkphp

藏色散人
Release: 2022-12-07 10:23:14
Original
2965 people have browsed it

Thinkphp method to convert timestamp: 1. Get the current date through "date('Y-m-d H:i:s');"; 2. Convert the specified date to "strtotime($str);" Timestamp; 3. Use "mktime" or "date_parse_from_format" to convert timestamp.

How to convert timestamp in thinkphp

The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.

How to convert timestamp in thinkphp?

ThinkPHP (current date, specified date) time and timestamp conversion

1. Year, month, day, hour, minute and second, time and timestamp conversion

public function test()
  {
    //获取当前日期
      echo '直接获取到的当前日期'.date('Y-m-d H:i:s');
      echo "\n";//换行
    //当前时间时间戳转日期的转换
      $time = time(); //获取当前时间戳
      echo '当前的时间戳'.$time;
      echo "\n";//换行
      $date = date('Y-m-d H:i:s', $time);//时间戳转换为日期格式
      echo '当前时间戳转换后日期'.$date;
      echo "\n";//换行
    //指定时间的转换  
      $str='1666146455';//指定时间戳
      echo '指定时间戳转换为时间'.date("Y-m-d H:i:s",$str);
      echo "\n";//换行
      $str='2022-10-19 11:50:50';//指定时间
      echo '指定日期转换为时间戳'.strtotime($str);
  }
Copy after login

Output results

How to convert timestamp in thinkphp

2. Conversion of year, month, day, time and timestamp

1. Transform directly through the above

public function test()
  {
    //获取当前日期
      echo '直接获取到的当前日期'.date('Y-m-d');
      echo "\n";//换行
    //当前时间时间戳转日期的转换
      $time = time(); //获取当前时间戳(包含时分秒)
      $date = date('Y-m-d', $time);//时间戳转换为日期格式
      echo '当前时间戳转换后日期'.$date;
      echo "\n";//换行
      echo '当前日期转换为时间戳'.strtotime($date);
      echo "\n";//换行
    //指定时间的转换  
      $str='1666146455';//指定时间戳
      echo '指定时间戳转换为时间'.date("Y-m-d ",$str);
      echo "\n";//换行
      $str='2022-10-19';//指定时间
      echo '指定日期转换为时间戳'.strtotime($str);
  }
Copy after login

Output result

How to convert timestamp in thinkphp

2. Convert through mktime, date_parse_from_format

public function test()
  {
    //当前日期的转换
        $date_new = date('Y-m-d'); //获取当前日期2022-10-19
        //日期转换为时间戳
        $arr = date_parse_from_format('Y-m-d', $date_new);
        $time = mktime(0, 0, 0, $arr['month'], $arr['day'], $arr['year']);
        echo '对应时间戳为:' . $time; 
        echo "\n";//换行
        //时间戳转换为日期
        $time1 = date("Y-m-d ", $time);
        echo '对应时间为:' . $time1; //2022-10-12
        echo "\n";
    //指定日期的转换
        $str = '2022-10-19'; //或者 2018年10月1日,中间的符号可以是任何
        $arr = date_parse_from_format('Y-m-d', $str); //如果是2022年10月19日,那么这里就是 Y年m月d日,上下需要保持一致
        $time = mktime(0, 0, 0, $arr['month'], $arr['day'], $arr['year']); //转换为时间戳
        echo '对应时间戳为:' . $time;
        echo "\n";
        //时间戳转换为日期
        $time1 = date("Y-m-d ", $time);
        echo '对应时间为:' . $time1;
  }
Copy after login

Output result

How to convert timestamp in thinkphp

Recommended Study: "thinkPHP Video Tutorial"

The above is the detailed content of How to convert timestamp in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template