commit 044c860 code中文原文

Loading Different CSS or JS Depending on the Time with PHP

Author
Yujia Lin
Date
Reading time
1 min read

Translated from Chinese by Claude. Read the original

On a whim, I tweaked my CSS so that it loads by time of day. The time here is Beijing time. Here’s the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php $hc = date("H");if ($hc >= 0 && $hc < 10) { ?> //判断时间

<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style.css" type="text/css" media="screen" />

<?php } else { ?>

<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/night.css" type="text/css" media="screen" />

<?php } ?>

{% endhighlight %}

其他非WordPress主题也适用(PHP),只要稍稍的修改一下子啦~

{% highlight PHP %}

<?php $hc = date("H");if ($hc >= 0 && $hc < 10) { ?> //判断时间

<link rel="stylesheet" href="你的CSS的路径/style.css" type="text/css" media="screen" />

<?php } else { ?>

<link rel="stylesheet" href="你的CSS的路径/night.css" type="text/css" media="screen" />

<?php } ?>

{% endhighlight %}

同时也可以判断时间加载不同的JS

{% highlight PHP %}

<?php $hc = date("H");if ($hc >= 0 && $hc < 10) { ?>

<script type="text/javascript" src="javascript路径"></script>

<?php } else { ?>

<script type="text/javascript" src="javascript路径"></script>

<?php } ?>
Esc

Keyboard shortcuts

Ctrl K /
Search and commands
j k
Next or previous post in a list
Enter
Open the selected post
h l
Older or newer post (on a post)
g h
Go to the log
g a
Go to the archive
g t
Go to tags
g s
Go to stats
t
Switch light or dark theme
r
Random post
?
Show this help