时间函数
Unix 时间戳
以32位整数表示的格林威治时间标准
1970 -- 2038
返回一个时间戳
Time();
获取当前时间信息 返回数组
Getdate(这里可以传一个时间戳,如果没有就返回当前的);
关联数组的键名含义
"seconds" | 秒的数字表示 | 0到 59 |
"minutes" | 分钟的数字表示 | 0到 59 |
"hours" | 小时的数字表示 | 0到 23 |
"mday" | 月份中第几天的数字表示 | 1到 31 |
"wday" | 星期中第几天的数字表示 | 0(表示星期天)到 6(表示星期六) |
"mon" | 月份的数字表示 | 1到 12 |
"year" | 4 位数字表示的完整年份 | 例如:1999 或 2003 |
"yday" | 一年中第几天的数字表示 | 0到 365 |
"weekday" | 星期几的完整文本表示 | Sunday到 Saturday |
"month" | 月份的完整文本表示 | January>到 December |
0 | 系统相关,典型值为从 -2147483648 到 2147483647。 |
取得精确的当前时间戳 返回一个包括时间戳 微秒的精确数组
Gettimeofday();
输出格式化后的时间
Date('Y-m-d H:i:s',time());
取得一个日期的unix时间戳
Mktime(小时,分钟,秒,月,日,年);
date('Y-m-d H:i:s',mktime(03,45,45,01,05,2001));
2001-01-05 03:45:45
计算年龄代码
//GET 1992-10-10
list($year,$match,$day) = explode('-',$_GET['date']);
$age = time() - mktime(0,0,0,$day,$match,$year);
echo '年龄是'.$age/(60*60*24*365);
设置时区
date_default_timezone_set("Asiz/ShangHai");
返回当前微秒数
microtime(true); 如果有参数 就返回一个浮点数 秒数+微妙
将任何英文文本描述成一个时间戳
Strtotime('-1 day');
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("+1 day"))
结果:2009-01-23 09:40:25
(2)打印昨天此时的时间戳strtotime("-1 day")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("-1 day"))
结果:2009-01-21 09:40:25
(3)打印下个星期此时的时间戳strtotime("+1 week")
当前时间:
1.echo date("Y-m-d H:i:s",time())
结果:2009-01-22 09:40:25
指定时间:
1.echo date("Y-m-d H:i:s",strtotime("+1 week"))
结果:2009-01-29 09:40:25
(4)打印上个星期此时的时间戳strtotime("-1 week")
php写一个日历类
<?php
header('Content-Type:text/html;charset=gbk');
date_default_timezone_set("Asiz/ShangHai");
?>
<style>
table {
border:1px solid #050;
}
th,.aaa {
background:blue;
color:#fff;
}
</style>
<?php
//知道本月第一天星期几 知道本月一共有多少天
class Calender {
private $year;
private $month;
private $days; //当前月一共多少天
private $start_weeksday; //当月第一天对应星期几
function __construct() {
//获取年月日
$this->year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$this->month =isset($_GET['month']) ? $_GET['month'] : date('m');
//本月第一天星期几
$this->start_weeksday = date('w',mktime(0,0,0,$this->month,1,$this->year));
//本月一共有多少天
$this->days = date('t',mktime(0,0,0,$this->month,1,$this->year));
}
function out () {
echo "<table>";
$this->changedate();
$this->weekslist();
$this->dayslist();
echo "</table>";
}
private function changedate() {
echo "<tr>";
echo '<td><a href="?'.$this->prevYear($this->year,$this->month).'"><<</a></td>';
echo '<td><a href="?'.$this->prevMonth($this->year,$this->month).'"><</a></td>';
echo "<td colspan='3'>".$this->year.'年'.$this->month."月</td>";
echo '<td><a href="?'.$this->nextMonth($this->year,$this->month).'">></a></td>';
echo '<td><a href="?'.$this->nextYear($this->year,$this->month).'">>></a></td>';
echo "</tr>";
}
private function prevYear($year,$month) {
$year = $year - 1;
if($year < 1970) {
$year = 1970;
}
return "&year={$year}&month={$month}";
}
private function prevMonth($year,$month) {
if($month == 1){
if($year < 1970) {
$year = 1970;
}
$year = $year - 1;
$month = 12;
} else {
$month--;
}
return "&year={$year}&month={$month}";
}
private function nextMonth($year,$month) {
if($month == 12){
if($year > 2038) {
$year = 2038;
}
$year = $year + 1;
$month = 1;
} else {
$month++;
}
return "&year={$year}&month={$month}";
}
private function nextYear($year,$month) {
$year = $year + 1;
if($year > 2038) {
$year = 2038;
}
return "&year={$year}&month={$month}";
}
private function weekslist () {
$week = array('日','一','二','三','四','五','六');
echo "<tr>";
for($i = 0;$i<count($week);$i++){
echo "<th>".$week[$i]."</th>";
}
echo "</tr>";
}
private function dayslist () {
echo "<tr>";
//输出第一天的空格
for($j=0;$j<$this->start_weeksday;$j++) {
echo "<td> </td>";
}
for($k=1;$k<=$this->days;$k++) {
$j++;
if($k == date('d')){
echo "<td class='aaa'>".$k."</td>";
} else {
echo "<td>".$k."</td>";
}
if($j%7==0) {
echo "</tr><tr>";
}
}
while($j%7!==0) {
echo "<td> </td>";
$j++;
}
echo "</tr>";
}
}
$a = new Calender();
$a->out();
?>