日期格式化date()函数的格式化字符:
Y 年,4位数字
m 月,前面带0:"01"-"12"
d 月中的天
H 时,24时制
i 分
s 秒
date("Y-m-d-H-i-s",时间戳)
显示的格式: 年-月-日 小时:分钟:妙
相关时间参数:
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"
- //导入自定义smarty操作类SmartyInit.php
- include_once('class/SmartyInit.php');
- $smarty = new SmartyInit();
- //设置默认时区为上海
- date_default_timezone_set('Asia/Shanghai');
- //输出echo strtotime('now'),结果如:1245763672
- //可知strtotime('now')返回的是时间戳
- //也可是从数据库得到的时间戳
- $time = time();
- echo 'php格式化输出:
'; - echo '昨天:'.date('Y-m-d H:i:s', strtotime('-1 day')).'
'; - //date('Y-m-d H:i:s'),不写第二个参数,默认为当前时间
- //也可写为:date('Y-m-d H:i:s', strtotime('now'))
- echo '今天:'.date('Y-m-d H:i:s').'
'; - echo '明天:'.date('Y-m-d H:i:s', strtotime('1 day')).'
'; - echo '赋值时间戳:'.date('Y-m-d H:i:s', $time).'
'; - //strtotime('today')只输出当天日期,
- //strtotime('today 00:00:00')可输出时间
- $smarty->assign('yesterday', strtotime('yesterday'));
- $smarty->assign('today', strtotime('today 20:15:04'));
- $smarty->assign('tomorrow', strtotime('tomorrow'));
- $smarty->assign('yesterday1', strtotime('-1 day'));
- //等同$smarty->assign('today1', strtotime('0 day'));
- $smarty->assign('today1', strtotime('now'));
- $smarty->assign('tomorrow1', strtotime('1 day'));
- $smarty->assign('time', $time);
- $smarty->display('index.html');