我有一个脚本可以在网站上显示雪我只需要添加调用js文件的代码
<script type="text/javascript" src="snowstorm.js"></script>
现在,我将每年从12月15日开始工作这条线路,并在1月15日停止
例:
If date >15 December && < 15 January { echo '<script type="text/javascript" src="snowstorm.js" ></script>'}
请帮忙吗?
此致敬意
使用DateTime
对象:
$now = new DateTime();
if ($now >= new DateTime('December 15') || $now <= new DateTime('January 15')) {
echo '<script src="snowstorm.js"></script>';
}
我在这里使用逻辑或||
,因为如果日期在今年 1 月 15 日之前或日期在今年 12 月 15 日之后,则应显示脚本。
这种情况在逻辑上变得稍微复杂一些,因为您必须考虑不同的年份。
试试这个
$date = time(); //current time. output timestamp format
date('n')==1?$currentYear=date('Y')-1:$currentYear=date('Y');
if($date > mktime(0, 0, 0, 12, 15, $currentYear) && $date < mktime(0, 0, 0, 1, 15,$currentYear+1))
{
echo '<script type="text/javascript" src="snowstorm.js" ></script>';
}
关于 PHP MKtime()
一种方法是在 index.html
/index.php
文件的顶部添加以下代码:
if(date("m.d") >= "12.15" && date("m.d") <= "01.15"){ // check the date
echo '<script type="text/javascript" src="snowstorm.js" ></script>'; // add the script if within the date range
}
阅读: date
|.PHP
使用 $.getScript():我们可以根据条件动态调用脚本插件。
$.getScript("http://code.jquery.com/jquery-1.9.1.js");
或者你可以像这样附加脚本
$('head').append($('<script>').attr('type', 'text/javascript').attr('src', '//code.jquery.com/jquery-1.9.1.js'));
要设置日期条件,请参阅此链接