使用PHP显示基于日期的内容 [英] Display content based on date with PHP
问题描述
<?php
//当前日期
$ date = date('Y,j,m');
//静态比赛开始日期
$ contestStart =('2012,02,03');
//如果当前日期是在比赛后开始
如果($ date> $ contestStart){
//比赛内容?
}
else {
// splash content?
}
?>
我觉得有更聪明的方法来做到这一点。我的网站是相当大的...包装整个事情在一个if语句看起来很可笑。可能根据日期重定向到不同的页面?
任何帮助都不胜感激。
您将要修改您的WordPress主题,以便更改可以在网站范围内。
- 登录到您的WordPress安装。
- 导航到外观 > 编辑器
- 在模板部分中,转到 header.php
- 在header.php的非常开头插入代码:
<?php
$ date = time();
$ contestStart = strtotime('2012-02-03 00:00:00');
if($ date< $ contestStart){
?>
< html>
在这里插入你的整个飞溅页面。
< / html>
<?php
exit;
}
?>
//正常的模板代码应该在这里。
不要忘记点击更新文件完成后WordPress上的按钮;和其他人一样,确保您指定的时间与服务器所在的时区同步。
I'm putting together a contest site built on Wordpress. Legally, the contest has to start at midnight. To avoid being up at midnight to set up the content, i'd like to build some PHP logic to show everything after the start date, and before then display some basic HTML for a splash page. I'm a TOTAL programming newb, but i'm trying to work the problem out, here's what I have so far:
<?php
// The current date
$date = date('Y, j, m');
// Static contest start date
$contestStart = ('2012, 02, 03');
// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}
else {
// splash content?
}
?>
I feel like there is smarter ways to do this. My site is fairly large... wrapping the whole thing in an if statement seems ridiculous. Maybe a redirect to a different page altogether based on the date?
Any help is appreciated.
You will want to modify your WordPress theme so that the changes can be site-wide.
- Log in to your WordPress installation.
- Navigate to Appearance > Editor
- In the Templates section, go to header.php
- At the very beginning of header.php, insert your code :
<?php
$date = time();
$contestStart = strtotime('2012-02-03 00:00:00');
if ($date < $contestStart) {
?>
<html>
Insert your whole splash page here.
</html>
<?php
exit;
}
?>
//The normal template code should be below here.
Don't forget to click the "Update File" button on WordPress when you're done; and like the others said, make sure that your specified time is synced with whatever timezone the server is in.
这篇关于使用PHP显示基于日期的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!