php时间戳

来源:互联网 发布:数据挖掘聚类常用方法 编辑:程序博客网 时间:2024/05/17 06:27

1.PHP 的 date() 函数用于格式化时间或日期。

1.1 Date() 函数可把时间戳格式化为可读性更好的日期和时间。

语法:

date(format,timestamp)
其中:
format:必需。规定时间戳的格式。

timestamp:可选。规定时间戳。默认是当前的日期和时间。


1.2 日期 - 什么是时间戳(TimeStamp)

时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数。它也被称为 Unix 时间戳(Unix Timestamp)。


1.3 日期 - 格式化日期

date() 函数的第一个参数规定了如何格式化日期/时间。它使用字母来表示日期和时间的格式。这里列出了一些可用的字母:
  • d - 月中的天 (01-31)
  • m - 当前月,以数字计 (01-12)
  • Y - 当前的年(四位数)

可以在字母之间插入其他字符,比如 "/"、"." 或者 "-",这样就可以增加附加格式了:
[php
  1. <?php  
  2. echo date("Y/m/d");  
  3. echo "<br />";  
  4. echo date("Y.m.d");  
  5. echo "<br />";  
  6. echo date("Y-m-d");  
  7. ?>  

以上代码的输出类似这样(今天的日期):


2014/05/02

2014.05.02

2014-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>
运行结果:



点击打开链接



0 0
原创粉丝点击