1.PHP 的 date() 函數用於格式化時間或日期。
1.1 Date() 函數可把時間戳格式化爲可讀性更好的日期和時間。
語法:
date(format,timestamp)其中:
format:必需。規定時間戳的格式。
timestamp:可選。規定時間戳。默認是當前的日期和時間。
時間戳是自 1970 年 1 月 1 日(00:00:00 GMT)以來的秒數。它也被稱爲 Unix 時間戳(Unix Timestamp)。
date() 函數的第一個參數規定了如何格式化日期/時間。它使用字母來表示日期和時間的格式。這裏列出了一些可用的字母:
- d - 月中的天 (01-31)
- m - 當前月,以數字計 (01-12)
- Y - 當前的年(四位數)
可以在字母之間插入其他字符,比如 "/"、"." 或者 "-",這樣就可以增加附加格式了:
- <?php
- echo date("Y/m/d");
- echo "<br />";
- echo date("Y.m.d");
- echo "<br />";
- echo date("Y-m-d");
- ?>
以上代碼的輸出類似這樣(今天的日期):
2014/05/02
2014.05.022014-05-02
</pre><p><span style="line-height:26px; font-family:'Microsoft YaHei'; font-size:18px"><strong>1.4 日期 - 添加時間戳</strong></span></p><p><span style="line-height:26px; font-family:'Microsoft YaHei'; font-size:18px">date() 函數的第二個參數規定了一個時間戳。此參數是可選的。如果您沒有提供時間戳,當前的時間將被使用。在我們的例子中,我們將使用 mktime() 函數爲明天創建一個時間戳。mktime() 函數可爲指定的日期返回時間戳。語法:</span></p><span style="font-family:Arial; font-size:14px; line-height:26px"></span><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:'Microsoft YaHei'; font-size:18px">mktime(hour,minute,second,month,day,year,is_dst)</span><span style="font-family:'Microsoft YaHei'; font-size:18px">;</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:'Microsoft YaHei'; font-size:18px"></span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:'Microsoft YaHei'; font-size:18px">time()函數用於返回當前時間的時間戳。</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:'Microsoft YaHei'; font-size:18px">語法:</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:'Microsoft YaHei'; font-size:18px"></span></p><div class="methodsynopsis dc-description"><span class="type">int</span> <span class="methodname"><strong>time</strong></span> ( <span class="methodparam">void</span> )</div><p></p><p class="para rdfs-comment">返回自從 Unix 紀元(格林威治時間 1970 年 1 月 1 日 00:00:00)到當前時間的秒數。</p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:'Microsoft YaHei'; font-size:18px">結合mktime()函數和time()函數,可計算出各種紀念日,如戀愛的時間。</span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"><span style="font-family:'Microsoft YaHei'; font-size:18px"></span></p><div class="methodsynopsis dc-description"><span class="type">int</span> <span class="methodname"><strong>time</strong></span> ( <span class="methodparam">void</span> )</div><p></p><p class="para rdfs-comment">返回自從 Unix 紀元(格林威治時間 1970 年 1 月 1 日 00:00:00)到當前時間的秒數。</p><p class="para rdfs-comment"></p><pre code_snippet_id="323312" snippet_file_name="blog_20140502_1_1143634" name="code" class="php"><html>
<META http-equiv="content-type" content="text/html; charset=utf-8">
<body>
<?php
date_default_timezone_set('UTC');
echo "當前時間:";
echo date('Y-n-j');
?>
<marquee direction="right" behavior="alternate" scrollamount=10 scrollamount=7>
<font style=font:12pt face=黑體 color=red>想知道在一起多久了?</font>
</marquee>
<marquee direction="down" scrollamount=7>
<font style=font:12pt face=黑體 color=green>輸入在一起的時間就告訴你~~</font>
</marquee>
<form action="welcome.php">
年:<input type="text" name="year"><br/>
月:<input type="text" name="month"><br/>
日:<input type="text" name="day"><br/>
<input type="submit" name="go">
</form>
</body>
</html>
welcome.php
<html>
<META http-equiv="content-type" content="text/html; charset=utf-8">
<body>
<?php
date_default_timezone_set('UTC');
$even = mktime(0,0,0,$_GET["month"],$_GET["day"],$_GET["year"]);
"\n";
$now = time();
$days = ($now-$even)/(60*60*24);
echo "已經在一起:".floor($days+1)."天";
//echo strtotime("$_GET['day'] $_GET['month'] $_GET['year']"), "\n";
?>
<br/>
<br/>
<br/>
<marquee direction="down" scrollamount=7>
<font style=font:12pt face=黑體 color=red>繼續保持哦~~~嘿嘿嘿</font>
</marquee>
<img src="1.jpg" height="400" width="500"/>
</body>
</html>
運行結果: