Home > Backend Development > PHP Tutorial > PHP timestamp and date format conversion

PHP timestamp and date format conversion

L
Release: 2023-04-08 17:32:01
forward
11729 people have browsed it

Summary of PHP timestamp and date conversion operations

1.Time conversion function in php

strtotime(date("Y-m-d H:i"))
date("Y-m-d H:i",$unixtime)
Copy after login

2. Get the timestamp of zero o'clock today in php

To get the unix timestamp of zero o'clock, you can use

$todaytime=strtotime("today")
Copy after login

and then use

date("Y-m-d H:i",$todaytime)
Copy after login

to convert is the date.

Convert timestamp to date

Time stamp conversion function:

date("Y-m-d H:i:s" ,time()), "Y-m-d H:i:s" is the converted date format, and time() is the timestamp to obtain the current time.

If it is date("Y-m-d H:i:s", time()), the hours, minutes and seconds will be displayed together;

If it is date("Y-m-d ", time()), Only the year, month and day are displayed.

For example:

date("Y-m-d H:i:s",time())
date("Y-m-d",time())
Copy after login

Convert date to timestamp

class SaonekController extends Controller {
 public function indexAction() {
  /*
  时间戳转换成日期不用说了
  但是日期要转成时间戳的话就要用到
  strtotime()
  */
  $time = time(); //时间戳
  $nowtime = date('Y-m-d H:i:s', $time); //生成带格式的日期
  $oldtime = '2010-11-10 22:19:21';
  $catime = strtotime($oldtime); //日期转换为时间戳
  $nowtimes = date('Y-m-d H:i:s', $catime); //时间戳又转回日期了
  echo $nowtimes;
 }
}
Copy after login

3. Time in php Convert stamp to date and display different contents according to time, such as just now, minutes ago, hours ago, today, yesterday, etc.

/*
时间转换函数
*/
function transTime($ustime) {
 $ytime = date("Y-m-d H:i", $ustime);
 $rtime = date("n月j日 H:i", $ustime);
 $htime = date("H:i", $ustime);
 $time = time() - $ustime;
 $todaytime = strtotime("today");
 $time1 = time() - $todaytime;
 if ($time < 60) {
  $str = &#39;刚刚&#39;;
 } else
  if ($time < 60 * 60) {
   $min = floor($time / 60);
   $str = $min . &#39;分钟前&#39;;
  } else
   if ($time < $time1) {
    $str = &#39;今天&#39; . $htime;
   } else {
    $str = $rtime;
   }
 return $str;
}
Copy after login

Other references

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")
Copy after login

Specified time:

echo date("Y-m-d H:i:s",strtotime("+1 day"))
Copy after login

(2) Print the PHP timestamp of yesterday at this time

strtotime("-1 day")
Copy after login

Specified time:

echo date("Y-m-d H:i:s",strtotime("-1 day"))
Copy after login

(3) Print the timestamp of next week at this time

strtotime("+1 week")
Copy after login

Specify time:

echo date("Y-m-d H:i:s",strtotime("+1 week"))
Copy after login

(4) Print the previous time The timestamp at this time of the week

strtotime("-1 week")
Copy after login

Specify the time:

echo date("Y-m-d H:i:s",strtotime("-1 week"))
Copy after login

(5) Print the PHP timestamp of the specified day of the next week

strtotime("next Thursday")
Copy after login

Specify time:

echo date("Y-m-d H:i:s",strtotime("next Thursday"))
Copy after login

(6) Print the timestamp of the specified day of the week

strtotime("last Thursday")
Copy after login

Specify time:

echo date("Y-m-d H:i:s",strtotime("last Thursday"))
Copy after login

Note: Don’t forget to set the time zone when using timestamp and date settings:

date_default_timezone_set(&#39;PRC&#39;); //设置中国时区
Copy after login

Recommended tutorial: "PHP Tutorial"



The above is the detailed content of PHP timestamp and date format conversion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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