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

PHP timestamp and date format conversion

L
Lforward
2020-05-27 17:27:1711703browse

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)

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")

and then use

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

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())

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;
 }
}

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;
}

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")

Specified time:

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

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

strtotime("-1 day")

Specified time:

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

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

strtotime("+1 week")

Specify time:

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

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

strtotime("-1 week")

Specify the time:

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

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

strtotime("next Thursday")

Specify 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")

Specify time:

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

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

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

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete