search
Homephp教程php手册PHP时间戳和日期相互转换

在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strtotime()函数实现,下面我来给大家举例说明。

1.php中时间转换函数

 代码如下 复制代码

strtotime(date())

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

2.php中获得今天零点的时间戳

要获得零点的unix时间戳,可以使用$todaytime=strtotime(“today”),

然后再使用date("Y-m-d H:i",$todaytime)转换为日期。

时间戳转换为日期 

时间戳转换函数:date("Y-m-d H:i:s",time()),"Y-m-d H:i:s"是转换后的日期格式,time()是获得当前时间的时间戳。如果是date("Y-m-d H:i:s",time()),则小时分秒一起显示;如果是date("Y-m-d ", time()),只显示年月日。例如:
date("Y-m-d H:i:s",time())转换后为:2010-07-18 18:42:48
date("Y-m-d",time())转换后为:2010-07-18

日期转换为时间戳 .

 代码如下 复制代码

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.php中时间戳转换为日期,并按照时间显示不同的内容,如刚刚,分钟前,小时前,今天,昨天等

 代码如下 复制代码

/*时间转换函数*/

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  
    $str = '刚刚';            
 
  }else if($time  
    $min = floor($time/60);                    
 
    $str = $min.'分钟前';            
 
  }else if($time  
    $str = '今天 '.$htime;            
 
  }else{                    
 
    $str = $rtime;            
 
  }              
 
  return $str;
 
}

其它的参考

使用date将当时间戳与指定时间戳转换成系统时间

(1)打印明天此时的时间戳strtotime(”+1 day“)

 代码如下 复制代码
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”+1 day”)) 结果:2009-01-23 09:40:25

(2)打印昨天此时的PHP时间戳strtotime(”-1 day“)

 代码如下 复制代码
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”-1 day”)) 结果:2009-01-21 09:40:25

(3)打印下个星期此时的时间戳strtotime(”+1 week“)

 代码如下 复制代码
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”+1 week”)) 结果:2009-01-29 09:40:25

(4)打印上个星期此时的时间戳strtotime(”-1 week“)

 代码如下 复制代码
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”-1 week”)) 结果:2009-01-15 09:40:25

(5)打印指定下星期几的PHP时间戳strtotime(”next Thursday“)

 代码如下 复制代码
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”next Thursday”)) 结果:2009-01-29 00:00:00

(6)打印指定上星期几的时间戳strtotime(”last Thursday“)

 代码如下 复制代码
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”last Thursday”)) 结果:2009-01-15 00:00:00



文章网址:

随意转载^^但请附上教程地址。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools