其实PHP中的日期实践函数“date()”非常的强大!看看吧:[语法]: string date(string format, int [timestamp]);
[传回值]: 字串
[内容说明]:
传回值的字串依设定的格式来决定。若有传入时间戳记值,则将时间戳记格式化传回;若无传入时间戳记值,则将目前服务器的时间格式化传回。要将日期转为其它的语系格式,应使用setlocale() 及 strftime() 二个函式。字串格式化的选项如下: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" 
其它不在上列的字元则直接列出该字元。 
 
使用范例 
范例一:
<?
print(date( "l dS of F Y h:i:s A" ));
print("July 1, 2000 is on a " . date("l", mktime(0,0,0,7,1,2000)));
?>范例二:
<?
$tomorrow  = mktime(0,0,0,date("m")  ,date("d")+1,date("Y"));
$lastmonth = mktime(0,0,0,date("m")-1,date("d"),  date("Y"));
$nextyear  = mktime(0,0,0,date("m"),  date("d",   date("Y")+1);
?>范例二:
<?php
echo date("Y-m-d");
//显示格式为:XXXX-XX-XX
//必须使用上面这种格式存入mysql的date字段中
echo date("Y-m-d H:i:s");
//显示格式为:XXXX-XX-XX XX:XX:XX
//必须使用上面这种格式存入mysql的datetime字段中
?>只要灵活运用“date()"函数的格式化字串,你想要什么都可以!!!

解决方案 »

  1.   


    create table  info (
      .....
      appear_time  datetime  default now(),
      .....
    )
    改为:
    create table  info (
      .....
      appear_time  datetime,
      .....
    )
    你可以通过"date()"函数来保存(反正你都要通过PHP来操作库)
    其实PHP中的日期实践函数“date()”非常的强大!看看吧:[语法]: string date(string format, int [timestamp]);
    [传回值]: 字串
    [内容说明]:
    传回值的字串依设定的格式来决定。若有传入时间戳记值,则将时间戳记格式化传回;若无传入时间戳记值,则将目前服务器的时间格式化传回。要将日期转为其它的语系格式,应使用setlocale() 及 strftime() 二个函式。字串格式化的选项如下: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" 
    其它不在上列的字元则直接列出该字元。 
     
    使用范例 
    范例一:
    <?
    print(date( "l dS of F Y h:i:s A" ));
    print("July 1, 2000 is on a " . date("l", mktime(0,0,0,7,1,2000)));
    ?>范例二:
    <?
    $tomorrow  = mktime(0,0,0,date("m")  ,date("d")+1,date("Y"));
    $lastmonth = mktime(0,0,0,date("m")-1,date("d"),  date("Y"));
    $nextyear  = mktime(0,0,0,date("m"),  date("d",   date("Y")+1);
    ?>范例二:
    <?php
    echo date("Y-m-d");
    //显示格式为:XXXX-XX-XX
    //必须使用上面这种格式存入mysql的date字段中
    echo date("Y-m-d H:i:s");
    //显示格式为:XXXX-XX-XX XX:XX:XX
    //必须使用上面这种格式存入mysql的datetime字段中
    ?>只要灵活运用“date()"函数的格式化字串,你想要什么都可以!!!
      

  2.   


    谢谢,用你的这种方法可以存取mysql的datetime字段。但有没有人知道mysql中如何设置日期的默认值么???就是create table  info (
      .....
      appear_time  datetime  default now(),
      .....
    )在sql server 7.0 中
    create table  info (
      .....
      appear_time  datetime  default getdate(),
      .....
    )
    是可以的呀。