PHP的日期和时间

UNIX时间戳

将日期和时间编程unix时间戳
            如果给定的年份是两位数格式,其值0~69表示2000~2069,70~100表示1970~2000。
 int mktime()

        若参数为空,默认将当前时间转变成unix时间戳,和直接调用time()函数获取当前UNIX时间戳功能相同。 

 

1
2
3
4
5
6
7
8
<?php
date_default_timezone_set( 'PRC' );
//日期超过31天,自动计算输出2009-01-05
echo date ( "Y-m-d" , mktime (0, 0, 0, 12, 36, 2008)). "\n" ;
//月份超过12,计算后输出2011-02-01
echo date ( "Y-m-d" , mktime (0, 0, 0, 14, 1, 2010)). "\n" ;
//会将99装变为1999年,1999-01-01
echo date ( "Y-m-d" , mktime (0, 0, 0, 1, 1, 99)). "\n" ;      

        如果需要将任何英文文本的日期时间描述直接解析为UNIX时间戳,可以使用strtotime()函数。可以接受一个包含美国英语日期格式的字符串并尝试将其解析为UNIX时间戳。执行成功,返回时间戳,否则返回false。

 

1
2
3
4
5
6
7
8
9
10
<?php
date_default_timezone_set( 'PRC' );
//2012-04-05
echo date ( "Y-m-d" , strtotime ( "now" )). "\n" ;
//2012-05-08
echo date ( "Y-m-d" , strtotime ( "8 may 2012" )). "\n" ;
//2012-04-06
echo date ( "Y-m-d" , strtotime ( "+1 day" )). "\n" ;
//2012-04-02
echo date ( "Y-m-d" , strtotime ( "lat monday" )). "\n" ;

在PHP中获取日期和时间

调用getdate()函数取得日期/时间信息

        getdate()函数返回一个有时间戳组成的关联数组,参数需要一个可选的UNIX时间戳。如果没有给出时间戳,则认为是当前本地时间。总共返回11个数组元素:

日期和时间格式化输出

        date()函数格式化一个本地时间和日期。第一个参数为时间戳的转换格式。第二个参数是可选的,需要提供一个UNIX时间戳。如果没有指定这个时间戳,将返回当前日期和时间。

1
2
3
4
<?php
date_default_timezone_set( "PRC" );
//输出2016年01月21日 09:01:38
echo date ( "Y年m月d日 H:i:s" );

 

修改PHP的默认时区

        PHP默认的时区设置是UTC时间,而北京正好位于时区的东八区,领先UTC8小时。如果希望正确第显示北京时间,就需要修改默认的市区设置:

 

  • 修改php.ini中data.timezone属性,可设为"Asia/shang"、"Etc/GMT-8"或PRC等中的一个,再在PHP脚本中获取当前时间就是北京时间。修改PHP的配置文件如下:

 

 

1
data.timezone = Etc/GMT-8

 

  • 如果使用的是共享服务器,没有权限修改配置文件,在php5.1.0以上,可以在输出时间之前调用date_defaulr_timezone_set()函数设置市区。该函数需要提供一个市区标识符作为参数。

 

 

1
date_default_timezone_set( "PRC" );

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

        调用microtime()返回当前UNIX时间戳和微秒数。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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对象
     
     $timer ->start();
     usleep(1000);
     $timer ->stop();
     
     echo "执行脚本用时<b>" . $timer ->spent(). "</b>秒" //执行脚本用时0.000x秒

日历类

Calendar.class.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
     date_default_timezone_set( "PRC" );
     /*
         file:calendar.class.php 日历类原文件
         声明一个日历类,名称为Calendar,用来显示一个可以设置日期的日历
     */
     class Calendar {
         private $year ;                    //当前的年
         private $month ;                   //当前的月
         private $start_weekday ;           //当月的第一天对应的是周几,作为当月开始遍历日期的开始
         private $days ;                    //当前月一总天数
 
         /* 构造方法,用来初使化一些日期属性  */
         function __construct(){
             /* 如果用户没有设置年份数,则使用当前系统时间的年份 */
             $this ->year = isset( $_GET [ "year" ]) ? $_GET [ "year" ] : date ( "Y" );
             /* 如果用户没有设置月份数,则使用当前系统时间的用份 */
             $this ->month = isset( $_GET [ "month" ]) ? $_GET [ "month" ] : date ( "m" );
             /* 通过具体的年份和月份,利用date()函数的w参数获取当月第一天对应的是周几 */
             $this ->start_weekday = date ( "w" , mktime (0, 0, 0, $this ->month, 1, $this ->year));
             /* 通过具体的年份和月份,利用date()函数的t参数获取当月的天数 */
             $this ->days = date ( "t" , mktime (0, 0, 0, $this ->month, 1, $this ->year));
         }
         
         /* 魔术方法用于打印整个日历 */
         function __toString(){
             $out .= '<table align="center">' ; //日历以表格形式打印
             $out .= $this ->chageDate();       //调用内部私有方法用于用户自己设置日期
             $out .= $this ->weeksList();       //调用内部私有方法打印“周”列表
             $out .= $this ->daysList();        //调用内部私有方法打印“日”列表
             $out .= '</table>' ;               //表格结束
             return $out ;                      //返回整个日历输出需要的全部字符串
         }
         
         /* 内部调用的私有方法,用于输出周列表 */
         private function weeksList(){
             $week = array ( '日' , '一' , '二' , '三' , '四' , '五' , '六' );
 
             $out .= '<tr>' ;
             for ( $i = 0; $i < count ( $week ); $i ++)
                 $out .= '<th class="fontb">' . $week [ $i ]. '</th>' ; //第一行以表格<th>形式输出周列表
 
             $out .= '</tr>' ;
             return $out ;                      //返回周列表字符串 
         }
         
         /* 内部调用的私有方法,用于输出日列表 */
         private function daysList(){
             $out .= '<tr>' ;
             /* 输出空格(当前一月第一天前面要空出来) */
             for ( $j = 0; $j < $this ->start_weekday; $j ++)
                 $out .= '<td>&nbsp;</td>' ;
 
             /* 将当月的所有日期循环遍历出来,如果是当前日期,为其设置深色背景 */
             for ( $k = 1; $k <= $this ->days; $k ++){
                 $j ++;
                 if ( $k == date ( 'd' ))
                     $out .= '<td class="fontb">' . $k . '</td>' ;
                 else
                     $out .= '<td>' . $k . '</td>' ;
                 
                 if ( $j %7 == 0)                        //每输出7个日期,就换一行
                     $out .= '</tr><tr>' ;             //输出行结束和下一行开始
             }
 
             while ( $j %7 !== 0){                       //遍历完日期后,将后面用空格补齐
                 $out .= '<td>&nbsp;</td>' ;           //使用空格去补
                 $j ++;
             }
 
             $out .= '</tr>' ;
             return $out ;                             //返回当月日期列表
         }
         
         /* 内部调用的私有方法,用于处理当前年份的上一年需要的数据 */
         private function prevYear( $year , $month ){
             $year = $year -1;                         //上一年是当前年减1
             
             if ( $year < 1970)                         //如果设置的年份小于1970年
                 $year = 1970;                        //年份设置最小值是1970年
 
             return "year={$year}&month={$month}" ;    //返回最终的年份和月份设置参数
         }
 
         /* 内部调用的私有方法,用于处理当前月份的上一月份的数据 */
         private function prevMonth( $year , $month ){
             if ( $month == 1) {                       //如果月份已经是1月
                 $year = $year -1;                   //则上一个月份,就是上一年的最后一月
         
                 if ( $year < 1970)                    //和前面一样,上一年如果是最1970年
                     $year = 1970;                   //最小年数不能小于1970年
 
                 $month =12;                          //如果月是1月,上一月就是上一年的最后一月
             } else {
                 $month --;                           //上一月就是当前月减1
             }
 
             return "year={$year}&month={$month}" ;   //返回最终的年份和月份设置参数
         }
 
         /* 内部调用的私有方法,用于处理当前年份的下一年份的数据 */
         private function nextYear( $year , $month ){
             $year = $year + 1;                       //下一年是当前年加1
 
             if ( $year > 2038)                         //如果设置的年份大于2038年
                 $year = 2038;                        //最大年份不能超过2038年
 
             return "year={$year}&month={$month}" ;    //返回最终的年份和月份设置参数
         }
 
         /* 内部调用的私有方法,用于处理当前月份的下一个月份的数据 */
         private function nextMonth( $year , $month ){
             if ( $month == 12){                        //如果已经是当年的最后一个月
                 $year ++;                             //下一个月就是下一年的第一个月,让年份加1年
 
                 if ( $year > 2038)                     //如果设置的年份大于2038年
                     $year = 2038;                    //最大年份不能超过2038年
 
                 $month = 1;                          //设置月份为下一年的第1月
             } else {
                 $month ++;                            //其它月份的下一个月都是当前月份加1即可
             }
             
             return "year={$year}&month={$month}" ;    //返回最终的年份和月份设置参数
         }
 
         //内部调用的私有方法,用于用户操作去调整年份和月份的设置
         private function chageDate( $url = "index.php" ){
             $out .= '<tr>' ;
             $out .= '<td><a href="' . $url . '?' . $this ->prevYear( $this ->year, $this ->month). '">' . '<<' . '</a></td>' ;
             $out .= '<td><a href="' . $url . '?' . $this ->prevMonth( $this ->year, $this ->month). '">' . '<' . '</a></td>' ;
             
             $out .= '<td colspan="3">' ;
             $out .= '<form>' ;
             $out .= '<select name="year" onchange="window.location=\'' . $url . '?year=\'+this.options[selectedIndex].value+\'&month=' . $this ->month. '\'">' ;
             for ( $sy =1970; $sy <= 2038; $sy ++){
                 $selected = ( $sy == $this ->year) ? "selected" : "" ;
                 $out .= '<option ' . $selected . ' value="' . $sy . '">' . $sy . '</option>' ;
             }
             $out .= '</select>' ;
             $out .= '<select name="month"  onchange="window.location=\'' . $url . '?year=' . $this ->year. '&month=\'+this.options[selectedIndex].value">' ;
             for ( $sm =1; $sm <=12; $sm ++){
                 $selected1 = ( $sm == $this ->month) ? "selected" : "" ;
                 $out .= '<option ' . $selected1 . ' value="' . $sm . '">' . $sm . '</option>' ;
             }
             $out .= '</select>' ;
             $out .= '</form>' ;   
             $out .= '</td>' ;
 
             $out .= '<td><a href="' . $url . '?' . $this ->nextYear( $this ->year, $this ->month). '">' . '>>' . '</a></td>' ;
             $out .= '<td><a href="' . $url . '?' . $this ->nextMonth( $this ->year, $this ->month). '">' . '>' . '</a></td>' ;
             $out .= '</tr>' ;
             return $out ;                              //返回调整日期的表单
         }
     }
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
     <head>
         <title>日历示例</title>
         <meta charset=utf-8 />
         <style>
             table { border:1px solid #050;}            /*给表格加一个外边框*/
             .fontb { color:white; background:blue;}    /*设置周列表的背景和字体颜色*/
             th { width:30px;}                          /*设置单元格子的宽度*/
             td,th { height:30px;text-align:center;}    /*设置单元高度,和字段显示位置*/
             form { margin:0px;padding:0px; }           /*清除表单原有的样式*/
         </style>
     </head>
     <body>
         <?php
             require "calendar.class.php" ;    //加载日历类
             echo  new Calendar;              //直接输出日历对象,自动调用魔术方法__toString()打印日历
         ?>
     </body>
</html>


 






转载于:https://www.cnblogs.com/staven/p/1f81e277f325504cb1dbdfe7b2406f4d.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值