网站地图    收藏   

主页 > php专栏 > php日期 >

php mysql日期操作函数 - php日期

来源:自学PHP网    时间:2014-11-30 11:50 作者: 阅读:

[导读] 文章利用了二个实例来分析介绍了关于php 日期和mysql日期的用法及格式还有包括了mysql使用日期时所用的字段类型及查询效率测试.php,int值:time():是返回自从 Unix 纪元(格林威治时间 1970 年...

php mysql日期操作函数

文章利用了二个实例来分析介绍了关于php 日期和mysql日期的用法及格式还有包括了mysql使用日期时所用的字段类型及查询效率测试.

php,int值:

time():是返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数.

我们想要获得1970 年 1 月 1 日到 2012-2-10的秒数可以通过strtotime()来实现:即:strtotime('2012-2-10');

date值:string date ( string format [, int timestamp] )

比如:直接date()返回的的实现当前的时间,当然我们可以指定的他的格式.例如date('Y-m-d',strtotime('2012-2-10'));

时间操作,代码如下:

  1. date('Y-m-d h:i:s',strtotime('+1 week')); 
  2. date('Y-m-d h:i:s',strtotime('+5 hours')); 
  3. date('Y-m-d h:i:s',strtotime('next Monday)); 
  4. date('Y-m-d h:i:s',strtotime('last Sunday')); 
  5. //开源代码phpfensi.com 
  6. date('Y-m-d h:i:s',strtotime('+ 1 day',12313223));!!详见 int strtotime ( string time [, int now] ) 

date的参数,每一个参数的格式分别表示:

a - "am" 或是 "pm" 

a - "am" 或是 "pm" 

d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31" 

d - 星期几,三个英文字母; 如: "fri" 

f - 月份,英文全名; 如: "january" 

h - 12 小时制的小时; 如: "01" 至 "12" 

h - 24 小时制的小时; 如: "00" 至 "23" 

g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12" 

g - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23" 

i - 分钟; 如: "00" 至 "59" 

j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31" 

l - 星期几,英文全名; 如: "friday" 

m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12" 

n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12" 

m - 月份,三个英文字母; 如: "jan" 

s - 秒; 如: "00" 至 "59" 

s - 字尾加英文序数,二个英文字母; 如: "th","nd" 

t - 指定月份的天数; 如: "28" 至 "31" 

u - 总秒数 

w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六) 

y - 年,四位数字; 如: "1999" 

y - 年,二位数字; 如: "99" 

z - 一年中的第几天; 如: "0" 至 "365" 

其它不在上列的字符则直接列出该字符.

(2)mysql,代码如下:

int->datetime

select from_unixtime(int_time) from table;

datetime->int;

select unix_timestamp(date_time) from table;

时间操作,代码如下:

select dayofweek('2012-2-2');返回一个星期的第几天

select dayofmonth('2012-2-2');返回一月中的第几天

select dayofyear('2012-2-2');返回一年中的第几天

类似函数:month() day() hour() week()......

+几天 date_add(date,interval 2 days);

-几天 date_sub(date,interval 2 days);

时间格式:date_format(date,format)

select DATE_FORMAT('1997-10-04 22:23:00','%W %M %Y');

其他函数:TIME_TO_SEC() SEC_TO_TIME()...

最后我们总结一下关于在使用时间时在数据库是是用int,varchar ,datetime这几种的查询效率比较吧.

测试四个表的更新,分别update 100条记录,并记录时间:北京PHP资源分享门户d*V.~x G/QO

表一:页面运行时间: 2.62180089951 秒(非定长,int时间)北京PHP资源分享门户

表二:页面运行时间: 2.5475358963 秒(定长,int时间)

表三:页面运行时间: 2.45077300072 秒(varchar,datetime时间)

表四:页面运行时间: 2.82798409462 秒(char,datetime时间)

大数据量下,如果存在大量的select * from table where 时间>XX这样的查询,在MySQL5.1时使用int换datetime是有意义的.

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论