Home > Article > PHP Framework > How to convert timestamp in thinkphp
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.
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); }
Output results
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); }
Output result
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; }
Output result
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!