複製鏈接
請複製以下鏈接發送給好友

strtotime

鎖定
strtotime,可以將美國英語格式時間字符串轉換的函數
中文名
strtotime
函數語法
int strtotime
函數作用
將美國英語格式時間字符串轉換
內置時間函數
(PHP 4, PHP 5)

strtotimePHP函數

PHP內置時間函數
(PHP 4, PHP 5)
strtotime可以用英語的自然語言創建某個時刻的時間戳
函數語法:int strtotime(string $time[,int $now])
函數作用:將美國英語格式的日期時間字符串轉換成unix時間戳。

strtotime函數例程

<?php
echo date("Y年n月j日",strtotime("now"));//2010年4月14日
echo date("Y年n月j日",strtotime("8 may 2008"));//2008年5月8日
echo date("Y年n月j日",strtotime("+1 day"));//2010年4月15日
echo date("Y年n月j日",strtotime("last monday"));//2010年4月12日
?>

strtotime實例

<?php
//紀念日倒記時
$time1=strtotime("2010-7-18 9:9:9");
$time2=strtotime("2010-4-18 23:42:10");
$second=$time1-$time2;
$year=floor($second/3600/24/365);//年
$temp=$second-$year*365*24*3600;
$month=floor($temp/3600/24/30);//月
$temp=$temp-$month*30*24*3600;
$day=floor($temp/3600/24);//日
$temp=$temp-$day*3600*24;
$hour=floor($temp/3600);//小時
$temp=$temp-$hour*3600;
$minute=floor($temp/60);//分
$second1=$temp-$minute*60;//秒
echo "距離培訓畢業還有".$year."年".$month."月".$day."天".$hour."小時".$minute."分".$second1."秒";
?>

strtotime基本信息

獲取英文文本日期時間 [1] 
示例如下:
便於比較,使用date將當時間戳與指定時間戳轉換成系統時間
(1)打印明天此時的時間戳strtotime("+1 day")
當前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("+1 day"))
結果:2009-01-23 09:40:25
(2)打印昨天此時的時間戳strtotime("-1 day")
當前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("-1 day"))
結果:2009-01-21 09:40:25
(3)打印下個星期此時的時間戳strtotime("+1 week")
當前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("+1 week"))
結果:2009-01-29 09:40:25
(4)打印上個星期此時的時間戳strtotime("-1 week")
當前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("-1 week"))
結果:2009-01-15 09:40:25
(5)打印指定下星期幾的時間戳strtotime("next Thursday")
當前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("next Thursday"))
結果:2009-01-29 00:00:00
(6)打印指定上星期幾的時間戳strtotime("last Thursday")
當前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("last Thursday"))
結果:2009-01-15 00:00:00
參考資料