PHP的日期和時間

來源:互聯網
上載者:User

標籤:php的日期和時間

一、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()."秒";

?>

五、日曆類

本文出自 “12145704” 部落格,轉載請與作者聯絡!

PHP的日期和時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.