PHP开发笔记系列(三)-日期与时间_PHP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > PHP > PHP开发笔记系列(三)-日期与时间

PHP开发笔记系列(三)-日期与时间

 2012/6/1 16:45:08  RYAN.D  程序员俱乐部  我要评论(0)
  • 摘要:前两篇完成了《PHP开发笔记系列(一)-PDO使用》和《PHP开发笔记系列(二)-字符串使用》,今天开始研究一下PHP中的日期时间处理和MySQL中的日期时间处理,《PHP开发笔记系列(三)-日期与时间》。日期时间是平常用得比较多的函数,在JAVA中我们通过newDate()可以获取服务器的当前时间,通过SimpleDateFormat类,并指定formatString,可以将date对象的值格式化成指定的形式。同理,在PHP中,同样有类似的类和函数,那就是time函数和date函数。time
  • 标签:笔记 开发笔记 PHP 开发

??? 前两篇完成了 《PHP开发笔记系列(一)-PDO使用》 和《PHP开发笔记系列(二)-字符串使用》 ,今天开始研究一下PHP中的日期时间处理和MySQL中的日期时间处理,《PHP开发笔记系列(三)-日期与时间》 。

?

????? 日期时间是平常用得比较多的函数,在JAVA中我们通过new Date()可以获取服务器的当前时间,通过SimpleDateFormat类,并指定formatString,可以将date对象的值格式化成指定的形式。同理,在PHP中,同样有类似的类和函数,那就是time函数和date函数。time()类似于new Date(),能够获取服务器当前时间,date函数类似于SimpleDateFormat类。

1. 使用time函数获取服务器当前时间,使用date函数进行格式化

file:time.php
url:http://localhost:88/datetime/timephp
<?php
    // 获取服务器当前时间
    $time = time();
   
    echo $time."<br/>";
   
    // 获取当前默认是时区
    echo date_default_timezone_get()."<br/>";
    echo date("Y-m-d h:i:s", $time)."<br/>";

    echo "<hr/>";
   
    // 设置当前默认是时区
    date_default_timezone_set("America/New_York");
    echo date("Y-m-d h:i:s", $time)."<br/>";
?>
?


2. 通过字符串构造日期时间

file:strtotime.php
url:http://localhost:88/datetime/strtotime.php
<?php  
    // 字符串转time类型
    $time1 = strtotime("2012-05-27 10:52:05");
    
    // 使用date函数获取星期几并输出
    echo 'today:'.date("l", $time1)."<br/>";
    
    // 使用date函数获取当月总天数并输出
    echo 'total day of this month:'.date("t", $time1)."<br/>";
?>
?

3. 基于当前时间的日期时间计算

file:date-compute.php
url:http://localhost:88/datetime/date-compute.php
<?php
    // 计算time
    $nextDay = strtotime("+1 day", time());
    $lastDay = strtotime("-1 day", time());
    $nextMonth = strtotime("+1 month", time());
    $lastMonth = strtotime("-1 month", time());
    $nextYear = strtotime("+1 year", time());
    $lastYear = strtotime("-1 year", time());
    
    echo 'new day:'.date("Y-m-d h:i:s", $nextDay)."<br/>";
    echo 'last day:'.date("Y-m-d h:i:s", $lastDay)."<br/>";
    echo 'next month:'.date("Y-m-d h:i:s", $nextMonth)."<br/>";
    echo 'last month:'.date("Y-m-d h:i:s", $lastMonth)."<br/>";
    echo 'next year:'.date("Y-m-d h:i:s", $nextYear)."<br/>";
    echo 'last year:'.date("Y-m-d h:i:s", $lastYear)."<br/>";
    
    // 通过date函数获取年份
    echo 'current year:'.date("Y");

?>

?
4. 在SQL中处理日期时间

SELECT NOW()
SELECT CURRENT_TIMESTAMP();
SELECT DATE_FORMAT(NOW(), "%Y-%m-%d");
SELECT DATE_FORMAT(NOW(), "%h:%i:%s");
SELECT DATE_FORMAT(NOW(), "%W %w %p");

?

5. 在SQL中进行日期计算
??? 使用DATE_ADD函数进行未来日期的计算

SELECT DATE_ADD(NOW(), INTERVAL 1 YEAR);
SELECT DATE_ADD(NOW(), INTERVAL 1 MONTH);
SELECT DATE_ADD(NOW(), INTERVAL 1 DAY);
SELECT DATE_ADD(NOW(), INTERVAL 1 HOUR);
SELECT DATE_ADD(NOW(), INTERVAL 1 MINUTE);
SELECT DATE_ADD(NOW(), INTERVAL 1 SECOND);

?

??? 使用DATE_SUB函数进行过去日期的计算

SELECT DATE_SUB(NOW(), INTERVAL 1 YEAR);
SELECT DATE_SUB(NOW(), INTERVAL 1 MONTH);
SELECT DATE_SUB(NOW(), INTERVAL 1 DAY);
SELECT DATE_SUB(NOW(), INTERVAL 1 HOUR);
SELECT DATE_SUB(NOW(), INTERVAL 1 MINUTE);
SELECT DATE_SUB(NOW(), INTERVAL 1 SECOND);

?


??? 上述函数的执行结合也可用于SQL语句当中,如查询前三天的日志信息,SQL如下:
SELECT * FROM log WHERE create_time BETWEEN SELECT DATE_SUB(NOW(), INTERVAL 3 DAY) AND SELECT DATE_ADD(NOW(), INTERVAL 1 DAY);

?

??? 本文地址:http://ryan-d.iteye.com/blog/1543363

?

  • datetime.zip (1.6 KB)
  • 下载次数: 0
发表评论
用户名: 匿名