PHP时间戳和日期相互转换docWord文档下载推荐.docx
《PHP时间戳和日期相互转换docWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《PHP时间戳和日期相互转换docWord文档下载推荐.docx(7页珍藏版)》请在冰点文库上搜索。
![PHP时间戳和日期相互转换docWord文档下载推荐.docx](https://file1.bingdoc.com/fileroot1/2023-5/5/3f4840b5-34f8-4567-85c5-aaf8ceef8c97/3f4840b5-34f8-4567-85c5-aaf8ceef8c971.gif)
2.php中获得今天零点的时间戳要获得零点的unix时间戳,可以使用
$todaytime=strtotime(“today”),
然后再使用
$todaytime)转换为日期。
时间戳转换为日期
时间戳转换函数:
i:
s"
time()),"
是转换后的日期格式,time()是获得当前时间的时间戳。
如果是date("
time()),则小时分秒一起显示;
如果是
Y-m-d"
time()),只显示年月日。
例如:
time())
转换后为:
2010-07-1818:
42:
48
Y-m-d"
2010-07-18日期转换为时间戳.
classSaonekControllerextendsController{
publicfunctionindex
Action
(){
/*
时间戳转换成日期不用说了
但是日期要转成时间戳的话就要用到
strtotime()
*/
$time=time();
//
时间戳
$nowtime=date(Y-m-dH:
s,$time);
生成带格式的日期
$oldtime=2010-11-1022:
19:
21;
$catime=strtotime($oldtime);
日期转换为时间戳
$nowtimes=date(Y-m-dH:
s,$catime);
时间戳又转回日期了
echo$nowtimes;
}
?
>
3.php中时间戳转换为日期,并按照时间显示不同的内容,如刚刚,分钟前,小时前,今天,昨天等
时间转换函数
functiontransTime($ustime){
$ytime=date("
$ustime);
$rtime=date("
n月j日H:
$htime=date("
H:
$time=time()-$ustime;
$todaytime=strtotime("
today"
);
$time1=time()-$todaytime;
if($time
$str=刚刚;
}elseif($time
$min=floor($time/60);
$str=$min.分钟前;
$str=今天.$htime;
}else{
$str=$rtime;
}
return$str;
其它的参考
使用date将当时间戳与指定时间戳转换成系统时间
(1)打印明天此时的时间戳
strtotime(”+1day“)
当前时间:
echodate(”Y-m-dH:
s”,time())
结果:
2009-01-2209:
40:
25
指定时间:
s”,strtotime(”+1day”))
2009-01-2309:
(2)打印昨天此时的
PHP
strtotime(”-1day“)
echodate(
”Y-m-dH:
s”,strtotime(”-1day”))
2009-01-2109:
(3)打印下个星期此时的时间戳
strtotime(”+1week“)
s”,strtotime(”+1week”))
2009-01-2909:
(4)打印上个星期此时的时间戳
strtotime(”-1week“)
s”,strtotime(”-1week”))
2009-01-1509:
(5)打印指定下星期几的PHP时间戳
strtotime(”nextThursday“)
s”,strtotime(”nextThursday”))
2009-01-2900:
00:
00
(6)打印指定上星期几的时间戳
strtotime(”lastThursday“)
s”,strtotime(”lastThursday”))
2009-01-1500: