使用PHP显示基于日期的内容 [英] Display content based on date with PHP

查看:98
本文介绍了使用PHP显示基于日期的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个建立在Wordpress上的竞赛网站组合在一起。在法律上,比赛必须从午夜开始。为了避免在午夜时间设置内容,我想建立一些PHP逻辑,以便在开始日期之后显示所有内容,然后在开始页面之前显示一些基本的HTML。我是一个TOTAL编程新手,但我正在努力解决这个问题,这里是我到目前为止:

  <?php 

//当前日期
$ date = date('Y,j,m');

//静态比赛开始日期
$ contestStart =('2012,02,03');

//如果当前日期是在比赛后开始
如果($ date> $ contestStart){
//比赛内容?
}

else {
// splash content?
}
?>

我觉得有更聪明的方法来做到这一点。我的网站是相当大的...包装整个事情在一个if语句看起来很可笑。可能根据日期重定向到不同的页面?



任何帮助都不胜感激。

解决方案

您将要修改您的WordPress主题,以便更改可以在网站范围内。


  1. 登录到您的WordPress安装。

  2. 导航到外观 > 编辑器

  3. 模板部分中,转到 header.php

  4. 在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.

  1. Log in to your WordPress installation.
  2. Navigate to Appearance > Editor
  3. In the Templates section, go to header.php
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆