PHP的日期和时间

发布时间:2020-06-18 18:58:01 作者:yuxin1234567890
来源:网络 阅读:408

一、UNIX时间戳

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

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

(1)mktime()函数

<?pph

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

?>

(2)strtotime()函数

<?php

echo date("Y-m-d",strtotime("now"))."<br/>";

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

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

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

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

<?php

$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

//在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

//使用微秒计算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()."秒";

?>

五、日历类

推荐阅读:
  1. PHP 日期和时间
  2. PHP 日期与时间

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 时间 日期

上一篇:http_banner获取v0.3

下一篇:域控NTP时间同步问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》