PHP的日期和时间-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
PHP的日期和时间

一、UNIX时间戳

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了湟源免费建站欢迎大家使用!

   以32位的整数表示格林威治标准时间。UNIX时间戳是从1970年1月1日零点开始起到当前时间所经历的秒数。

1、将日期和时间转变成UNIX时间戳

(1)mktime()函数

echo date("Y-m-d-h-m-s",mktime(12,10,56,12,67,2016))."\n";

?>

(2)strtotime()函数

echo date("Y-m-d",strtotime("now"))."
";

echo date("Y-m-d",strtotime("10 may 2014"))."
";

echo date("Y-m-d",strtotime("+2 day"))."
";

echo date("Y-m-d",strtotime("last monday"));

?>
(3)实例:用strtotime()函数编写一个纪念日的倒计时程序

$now=strtotime("now");

$endtime=strtotime("2018-6-19 23:59:59");

$second=$endtime-$now;

$year=floor($second/60/60/24/365);

$temp=$second-$year*365*24*60*60;

$month=floor($temp/60/60/24/30);

$temp=$temp-$month*30*24*60*60;

$day=floor($temp/60/60/24);

$temp=$temp-$day*24*60*60;

$hour=floor($temp/60/60);

$temp=$temp-$hour*60*60;

$minute=floor($temp/60);

$second1=$temp-$minute*60;

echo "距离大学毕业还有".$year."年".$month."月".$day."日".$hour."小时".$minute."分".$second1."秒";


2、日期的计算


//在PHP脚本中接收HTML表单用户提交的出生日期,计算这个用户的年龄

$year=1992;

$month=11;

$day=2;

$birthday=mktime(0,0,0,$month,$day,$year);       //将出生日期转变为UNIX时间戳

$nowdate=time();

$ageunix=$nowdate-$birthday;

$age=floor($ageunix/60/60/24/365);

echo "您的年龄是".$age."岁";

?>

二、在PHP中获取日期和时间

三、修改PHP默认时区

四、使用微秒计算PHP脚本执行时间

//使用微秒计算PHP脚本执行时间

class Timer{

private $startTime=0;

private $stopTime=0;

function start(){

$this->startTime=microtime(true);

}

function stop(){

$this->stopTime=microtime(true);

}

function spent(){

return round(($this->stopTime-$this->startTime),4);

}

}

$Timer=new Timer();

$Timer->start();

usleep(2000);

$Timer->stop();

echo "执行该脚本用时".$Timer->spent()."秒";

?>

五、日历类


分享标题:PHP的日期和时间
URL链接:http://scgulin.cn/article/gpjose.html