htaccess - 如何获得昨天的时间戳?

时间:2015-04-16 15:14:47

标签: .htaccess

我试图使用以下环境变量获取昨天在htaccess中的时间戳

TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME

我正考虑使用多个TIME_DAY从当前RewriteCond中减去1,如:

RewriteCond %{TIME_DAY} ^02$
RewriteRule ^.* - [E=TIME_PREV_DAY:01,E=TIME_NEXT_DAY:03]

RewriteCond %{TIME_DAY} ^03$
RewriteRule ^.* - [E=TIME_PREV_DAY:02,E=TIME_NEXT_DAY:04]

但是如何处理每个月的天数和月份的变化?

2 个答案:

答案 0 :(得分:1)

以下htaccess可以处理月份的第一天而不是年份:

#-----------first day of month------------

RewriteCond %{TIME_MON}%{TIME_DAY} ^0101$
RewriteRule ^.* - [E=TIME_YESTERDAY:1231]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0201$
RewriteRule ^.* - [E=TIME_YESTERDAY:0131]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0301$
RewriteRule ^.* - [E=TIME_YESTERDAY:0228]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0401$
RewriteRule ^.* - [E=TIME_YESTERDAY:0331]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0501$
RewriteRule ^.* - [E=TIME_YESTERDAY:0430]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0601$
RewriteRule ^.* - [E=TIME_YESTERDAY:0531]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0701$
RewriteRule ^.* - [E=TIME_YESTERDAY:0630]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0801$
RewriteRule ^.* - [E=TIME_YESTERDAY:0731]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0901$
RewriteRule ^.* - [E=TIME_YESTERDAY:0831]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1001$
RewriteRule ^.* - [E=TIME_YESTERDAY:0930]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1101$
RewriteRule ^.* - [E=TIME_YESTERDAY:1031]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1201$
RewriteRule ^.* - [E=TIME_YESTERDAY:1130]

#-----------rest of month------------

RewriteCond %{TIME_DAY} ^02$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}01]

RewriteCond %{TIME_DAY} ^03$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}02]

RewriteCond %{TIME_DAY} ^04$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}03]

RewriteCond %{TIME_DAY} ^05$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}04]

RewriteCond %{TIME_DAY} ^06$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}05]

RewriteCond %{TIME_DAY} ^07$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}06]

RewriteCond %{TIME_DAY} ^08$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}07]

RewriteCond %{TIME_DAY} ^09$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}08]

RewriteCond %{TIME_DAY} ^10$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}09]

RewriteCond %{TIME_DAY} ^11$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}10]

RewriteCond %{TIME_DAY} ^12$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}11]

RewriteCond %{TIME_DAY} ^13$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}12]

RewriteCond %{TIME_DAY} ^14$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}13]

RewriteCond %{TIME_DAY} ^15$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}14]

RewriteCond %{TIME_DAY} ^16$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}15]

RewriteCond %{TIME_DAY} ^17$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}16]

RewriteCond %{TIME_DAY} ^18$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}17]

RewriteCond %{TIME_DAY} ^19$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}18]

RewriteCond %{TIME_DAY} ^20$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}19]

RewriteCond %{TIME_DAY} ^21$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}20]

RewriteCond %{TIME_DAY} ^22$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}21]

RewriteCond %{TIME_DAY} ^23$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}22]

RewriteCond %{TIME_DAY} ^24$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}23]

RewriteCond %{TIME_DAY} ^25$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}24]

RewriteCond %{TIME_DAY} ^26$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}25]

RewriteCond %{TIME_DAY} ^27$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}26]

RewriteCond %{TIME_DAY} ^28$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}27]

RewriteCond %{TIME_DAY} ^29$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}28]

RewriteCond %{TIME_DAY} ^30$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}29]

RewriteCond %{TIME_DAY} ^31$
RewriteRule ^.* - [E=TIME_YESTERDAY:%{TIME_MON}30]

答案 1 :(得分:1)

Writeup of RewriteRules to get yesterdays time

以下测试代码会将变量TIME_YESTERDAY设置为当前时间减去24小时。

#-----------first day of month------------
# skip next 12 rewriterules if time_day is not 01
RewriteCond %{TIME_DAY} !^01$
RewriteRule .* - [S=12]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0101$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}1231%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=10]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0201$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0131%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=9]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0301$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0228%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=11]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0401$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0331%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=8]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0501$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0430%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=3]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0601$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0531%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=7]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0701$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0630%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=2]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0801$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0731%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=6]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0901$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0831%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=5]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1001$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0930%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=1]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1101$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}1031%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=4]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1201$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}1130%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC}]

#-----------rest of month------------
# skip next 30 rewriterules if time_day is 01
RewriteCond %{TIME_DAY} ^01$
RewriteRule .* - [S=30]

RewriteCond %{TIME_DAY} ^02$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}01%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=29]

RewriteCond %{TIME_DAY} ^03$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}02%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=28]

RewriteCond %{TIME_DAY} ^04$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}03%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=27]

RewriteCond %{TIME_DAY} ^05$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}04%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=26]

RewriteCond %{TIME_DAY} ^06$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}05%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=25]

RewriteCond %{TIME_DAY} ^07$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}06%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=24]

RewriteCond %{TIME_DAY} ^08$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}07%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=23]

RewriteCond %{TIME_DAY} ^09$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}08%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=22]

RewriteCond %{TIME_DAY} ^10$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}09%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=21]

RewriteCond %{TIME_DAY} ^11$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}10%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=20]

RewriteCond %{TIME_DAY} ^12$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}11%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=19]

RewriteCond %{TIME_DAY} ^13$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}12%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=18]

RewriteCond %{TIME_DAY} ^14$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}13%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=17]

RewriteCond %{TIME_DAY} ^15$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}14%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=16]

RewriteCond %{TIME_DAY} ^16$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}15%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=15]

RewriteCond %{TIME_DAY} ^17$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}16%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=14]

RewriteCond %{TIME_DAY} ^18$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}17%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=13]

RewriteCond %{TIME_DAY} ^19$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}18%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=12]

RewriteCond %{TIME_DAY} ^20$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}19%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=11]

RewriteCond %{TIME_DAY} ^21$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}20%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=10]

RewriteCond %{TIME_DAY} ^22$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}21%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=9]

RewriteCond %{TIME_DAY} ^23$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}22%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=8]

RewriteCond %{TIME_DAY} ^24$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}23%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=7]

RewriteCond %{TIME_DAY} ^25$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}24%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=6]

RewriteCond %{TIME_DAY} ^26$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}25%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=5]

RewriteCond %{TIME_DAY} ^27$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}26%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=4]

RewriteCond %{TIME_DAY} ^28$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}27%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=3]

RewriteCond %{TIME_DAY} ^29$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}28%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=2]

RewriteCond %{TIME_DAY} ^30$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}29%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=1]

RewriteCond %{TIME_DAY} ^31$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}30%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC}]