Home >Backend Development >PHP Problem >How to convert php timestamp to t format

How to convert php timestamp to t format

藏色散人
藏色散人Original
2023-03-20 11:02:142898browse

How to convert php timestamp to t format: 1. Create a php sample file; 2. Use the "date('Y-m-dTH:i:sZ', 1547740800);" method or "date( 'c',1547740800);" method can convert unix timestamp to UTC.

How to convert php timestamp to t format

The operating environment of this tutorial: Windows 10 system, PHP8.1, Dell G3 computer.

How to convert php timestamp to t format?

PHP converts UNIX timestamp UTC time (TZ format) Standard time method UNIX UTC GMT time and local time conversion

We often encounter various differences in PHP programming Format time

For example:

unix时间戳:1547740800
UTC时间:2019-1-18T00:00:00Z
标准时间:2019-1-18 00:00:00

The above three time formats all represent 0:00 on January 18, 2019 Minutes and seconds, how to convert these three time formats in PHP? Here we will teach you

1. Convert UNIX to UTC

echo date('Y-m-dTH:i:sZ', 1547740800);
//输出:2019-1-18T00:00:00Z

or

echo date('c',1547740800);
//输出:2019-1-18T00:00:00Z+02:00

2. Convert UNIX to standard time

echo date('Y-m-d H:i:s', 1547740800);
//输出:2019-1-18 00:00:00

3. Convert UTC to standard time

   $utc='2019-1-18T00:00:00Z';
   $unix= str_replace(array('T','Z'),' ',$utc);
   echo $unix;
  //输出:2019-1-18 00:00:00

4. Convert UTC to UNIX timestamp

 $utc='2019-1-18T00:00:00Z';
   $time= str_replace(array('T','Z'),' ',$utc);
   $unix=strtotime($time);
   echo $unix;
  //输出:1547740800

5. Convert standard time to UNIX timestamp

   $time='2019-1-18 00:00:00';
   $unix=strtotime($time);
   echo $unix;
   //输出:1547740800

6. Convert standard time to UTC time

   $time='2019-1-18 00:00:00';
   $unix=strtotime($time);
   $utc=date('Y-m-dTH:i:sZ', $unix);
   echo $utc;
   //输出:2019-1-18T00:00:00Z

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert php timestamp to t format. 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