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.
I suppose technically your code's logic would work but yes, there are much better ways of doing this. However, for your purpose we will go simple.
You should be comparing against a timestamp and not a string. Try this:
<?php
// The current date
$date = time();
// Static contest start date
$contestStart = strtotime('2012-02-03 00:00:00');
// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}
else {
// splash content?
}
?>
You can put the splash and content components in separate .php files, and simply include() then within the conditionals.
if ($date > $contestStart) {
include("SECRETDIR/contest.php");
}
else {
include("SECRETDIR/splash.php");
}
You'll want to set up an .htaccess so that people cannot point their browsers towards secretdir and directly access contest.php
You don't have to wrap the content, you can have something like:
<?php
if ( time() < strtotime('2012-02-03 00:00:00') ) {
echo "not yet!";
exit;
}
//code here wont be executed
Be careful of timezones! Your server might be in a different timezone than your content.
// setup the start date (in a nice legible form) using any
// of the formats found on the following page:
// http://www.php.net/manual/en/datetime.formats.date.php
$contestStart = "February 1, 2012";
// check if current time is before the specified date
if (time() < strftime($contestStart)){
// Display splash screen
}
// date has already passed
else {
// Display page
}
Related
I presently have a situation whereby I need to show the new copy in a staging area for review in current time. On the live site this new copy needs to show on a set time and date in the future.
For example:
preview.webaddress.co.uk (staging area) to show new copy.
www.webaddress.co.uk (live area) not to show until said date and time.
This is how it looks at present:
<?php
date_default_timezone_set("europe/london");
$current_date = intval(date("YmdHi"));
if(201701011000 < $current_date ) {
?>
<?php include('new_copy_for_new_time_to_add.php');?>
<?php } ?>
<?php include('existing_copy_which_stays.php');?>
I've made amendments to above code and added more info to help.
i don't know your problem.
but you need
delete final line
?>
I'm looking at using a large image in the background of a home page and would like the image to change every day, using CSS only. I'll obviously have a set of images to call in, not sure how may yet. It would be good if they could loop through though.
Would it be worth using a sprite and shifting the position on a 24 hour loop? I'm sure there's a cleaner way...
Many thanks!
You cannot to this using CSS only.
You can of course create 24 classes, one for each hour (or day, or whatever you like), each having a separate image background:
.hour01 { background:url('image-01.png'); }
.hour02 { background:url('image-02.png'); }
etc....
But you need either a server side language (such as PHP) or a client side language (javascript) to check what date/time it is and based on that, switch the CSS class of the body element.
You can't do that only in css but if you add some js it's doable :
today = new Date(); // will return 1,2,...,7 correpsonding to week days
if (today.getDay() === 1){
document.getElementById("background").className += 'mondayStyle';
}
if (today.getDay() === 2){
document.getElementById("background").className += 'tuersdayStyle';
}
...
Edit:
If you have not exactly 7 images (but less than ~30) you can do :
var nbOfImages = 12;
numDay = (today.getDate())% nbOfImages; // day of the month modulus nbOfImages
if (numDay === 1){
document.getElementById("background").className += 'tuersdayStyle';
}
...
I have searched the web for an answer to my question, but with limited success. I am a real newbie when it comes to programming languages. Here's my question: I have one HTML page designed with a "daytime" theme and another with a "nighttime" theme. Then I have a third HTML page where I want to load one of these two WHOLE pages into this third page (not into a div or iframe in the third page), according to the time of day. I want to load the "daytime" theme page during the morning and afternoon hours, and the "nighttime" theme page during the evening/nighttime hours.
I have the following PHP code, but it is not working for me:
<?php
$time = date('G');
if($time >=0 || $time < 6)
{
$page = 'indexnight.html';
}
if($time >=6 || $time < 18)
{
$page = 'indexday.html';
}
if($time >=18 || $time < 24)
{
$page = 'indexnight.html';
}
include ('$page');
?>
Can someone check over this and locate any problems with this coding? Also, I need to know where this code should be placed in my third HTML page and if there is any additional coding that needs to go within the body tags of the page.
Thanks for any help you can give me.
hey you used single Quote include ('$page'); Instead use include ("$page");
Shouldn't you control the themes by CSS (which is designed for appearance) rather than HTML (which is designed for content)? But ignoring that, you should do something like:
if ($time >=6 and $time < 18) { readfile('indexday.html');}
else { readfile('indexnight.html');}
And that should replace pretty much all your code (except for the $time definition).
I have a website that has a Radio schedule, Mon - Sun. I would like to be able to display the correct schedule page for the current day, how would I do this? The site is a standard html site. I would also like to be able to highlight the particular show depending on the time of day.
Any ideas, this is all new to me.
Regards
Gary
Imagine that you have the following html documents:
Monday.html
...
Sunday.html
You can use the following code to output the page of the day:
<?php
$page = date('l') . '.html';
readfile($page);
exit(0);
?>
The example uses the function date() to get the day of the week as textual representation.
Place the code in file called pageOfTheDay.php in the same folder as the html documents on your webserver. In your HTML then use a link like
Page of the day
Note: The example assumes that you are using an english locale.
since it's also in the javascript section I'll post a JS version of it:
var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Weekend","Weekend"];
var d = new Date();
var n = d.getDay();
window.location = days[n - 1]+".html";
place this at the top of your index.html file. In the same folder you need the various Monday.html ... Friday.html files
in the same way you could do:
var d = new Date();
var h = d.getHours();
and then add a class "current-show" to a specific element in the page (with jQuery)
$(".show-time-"+ h).addClass("current-show");
This assumes that you have given the right classes to the show in the page (for example
<div class="show-time-14"></div>
And then in your css you can do:
.current-show { background-color: #BADA55: }
You could benefit from a "page controller", say index.php. This page is the only page the user loads and the actual HTML "page" is included automatically. This could be decided by a URL param or default to the current date.
An untested and very basic example of what I mean:
<?php
if (isset($_GET["day"])) {
$day = $_GET["day"];
} else {
$day = date("l");
}
$fileName = $day . ".php";
if (file_exists(realpath() . $fileName)) {
include $fileName;
}
?>
Note: I have renamed the .HTML files to .PHP, allowing them to be included (you don't actually need to have any PHP code within them.
I guess, this is a very simple question. I want my wordpress theme to automatically change some global variables on a given date.
My Theme changes color and some other things every two months. From now on, I want to type in the needed variables before that date and let Wordpress do the changes. I could do that from within the loop, so that the first person who enters the site on that date initiates the change. But that would mean extra code, everytime the loop is called. Is it possible to perform that task automatically?
I haven't used WP in ages, but I think this will work, change time_to_change_theme to the forward date you want. I have no idea where to put this, I am sure you will work it out though.
<?php
$time_to_change_theme = strtotime("2012-12-31 12:12:12"); // the time in the future you want to change the theme
$time_now = strtotime(now);
if($time_to_change_theme > $time_now)
{
echo "Use current theme";
}else{
echo "Change theme";
update_option('current_theme', '[theme name]'); // this should update the current theme
}
?>
Uses code from this post by harmen