PHP触发日期?圣诞网站准备
PHP Trigger date? xmas website preparation
我正在为 11 月 1 日至 12 月 31 日午夜的圣诞节期间准备我们的企业网站。我已经在站点 CSS style-sheet 上进行了修改,以便在将 "xmas" 的 class 添加到 body 元素时触发圣诞主题覆盖,如果已放置我将此代码放入我们的全局 head.php 文件中,但它现在正在触发,我正试图在 PHP 标签中捕获它以使用日期函数触发?
这可能吗 - 如果可以,有人可以帮忙吗?
到目前为止我已经试过了...
<?php
//Must be in format of DAY-MONTH-YEAR
if(date('d-j-Y') == "30-11-date('Y')") {
?>
<script>
$(document).ready(function(){
$('body').addClass('xmas');
});
</script>
<?php
}
?>
当然这只会在 11 月 30 日有效,我需要它在 12 月 31 日之前有效。
您可以使用带有月份和日期的日期函数,如下所示:
$date = date('md');
if($date >= '1130' && $date <= '1231')
使用 DateTime
对象,它们是可比较的并且使这相当简单和直接。
<?php
$now = new DateTime();
$nov1 = new DateTime('2015-11-01 00:00:00');
$dec31 = new DateTime('2015-12-31 23:59:59');
if($now >= $nov1 && $now <= $dec31) {
?>
如果您想让代码每年都动态化,只需像示例中那样使年份值动态化。
要有一个脚本,无论是年份还是日期,它总是告诉你圣诞节和除夕假期的时间
我在PHP
中使用过它
/*
* ==12 if is decembre, >= 17 if day is great than 17
* ==12 if is january, <= 7 if day is less than 7
* @return if is holiday time or not
*/
public static function isHolidayTime()
{
if(date("m") == 12 && date("d") >= 17){
//Holiday time there
return true;
}else if(date("m") == 1 && date("d") <= 7){
//Holiday time there
return true;
}
else{
//normal day
return false;
}
}
我正在为 11 月 1 日至 12 月 31 日午夜的圣诞节期间准备我们的企业网站。我已经在站点 CSS style-sheet 上进行了修改,以便在将 "xmas" 的 class 添加到 body 元素时触发圣诞主题覆盖,如果已放置我将此代码放入我们的全局 head.php 文件中,但它现在正在触发,我正试图在 PHP 标签中捕获它以使用日期函数触发?
这可能吗 - 如果可以,有人可以帮忙吗?
到目前为止我已经试过了...
<?php
//Must be in format of DAY-MONTH-YEAR
if(date('d-j-Y') == "30-11-date('Y')") {
?>
<script>
$(document).ready(function(){
$('body').addClass('xmas');
});
</script>
<?php
}
?>
当然这只会在 11 月 30 日有效,我需要它在 12 月 31 日之前有效。
您可以使用带有月份和日期的日期函数,如下所示:
$date = date('md');
if($date >= '1130' && $date <= '1231')
使用 DateTime
对象,它们是可比较的并且使这相当简单和直接。
<?php
$now = new DateTime();
$nov1 = new DateTime('2015-11-01 00:00:00');
$dec31 = new DateTime('2015-12-31 23:59:59');
if($now >= $nov1 && $now <= $dec31) {
?>
如果您想让代码每年都动态化,只需像示例中那样使年份值动态化。
要有一个脚本,无论是年份还是日期,它总是告诉你圣诞节和除夕假期的时间
我在PHP
中使用过它 /*
* ==12 if is decembre, >= 17 if day is great than 17
* ==12 if is january, <= 7 if day is less than 7
* @return if is holiday time or not
*/
public static function isHolidayTime()
{
if(date("m") == 12 && date("d") >= 17){
//Holiday time there
return true;
}else if(date("m") == 1 && date("d") <= 7){
//Holiday time there
return true;
}
else{
//normal day
return false;
}
}