• Resolved modestkate

    (@modestkate)


    Hi everyone,

    I am trying to make the header image on my blog ( http://www.areyoumodest.com ) change three different times throughout the day, for morning, afternoon, and night depending on where the user is. I attempted downloading a plugin to do this, but had trouble finding any that were customizable by the hour. Can anyone help me determine how to do this via handcoding?

    Any tips would really be appreciated!! =]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Checking the hour is simple, checking the timezone is not very reliable…

    You could use this to check the hour, how you handle the timezone is up to you…

    You need to take in consideration how to handle timezone, if you check based on IP there’s a number of unreliable factors such as being behind a proxy.

    [PHP]

    $thehour = date('H');
    
    switch($thehour)
    {
      case '00':echo 'The hour equals 00';break;
      case '01':echo 'The hour equals 01';break;
      case '02':echo 'The hour equals 02';break;
      case '03':echo 'The hour equals 03';break;
      case '04':echo 'The hour equals 04';break;
      case '05':echo 'The hour equals 05';break;
      case '06':echo 'The hour equals 06';break;
      case '07':echo 'The hour equals 07';break;
      case '08':echo 'The hour equals 08';break;
      case '09':echo 'The hour equals 09';break;
      case '10':echo 'The hour equals 10';break;
      case '11':echo 'The hour equals 11';break;
      case '12':echo 'The hour equals 12';break;
      case '13':echo 'The hour equals 13';break;
      case '14':echo 'The hour equals 14';break;
      case '15':echo 'The hour equals 15';break;
      case '16':echo 'The hour equals 16';break;
      case '17':echo 'The hour equals 17';break;
      case '18':echo 'The hour equals 18';break;
      case '19':echo 'The hour equals 19';break;
      case '20':echo 'The hour equals 20';break;
      case '21':echo 'The hour equals 21';break;
      case '22':echo 'The hour equals 22';break;
      case '23':echo 'The hour equals 23';break;
    
    }

    Of course you don’t have to use echoes, you can set a variable if you prefer then display the value of the variable after the switch… or any number of other things…

    Another example..

    [PHP]

    $thehour = date('H');
    
    switch($thehour) {
    case '00': $myvar = 'something';break;
    case '01': $myvar = 'something else';break;
    case '02': $myvar = 'something else again';break; // and so on up to 23 as previous example, for each hour of the day...
    }
    
    echo '<div class="somediv">'.$myvar.'</div>';

    If the hour was 01 (as in 1am) you’d get <div class="somediv">something else</div>

    Hope that helps… πŸ™‚

    Actually i think it can be done a little easier using something like this..

    $thehour = date('G');
    
    foreach (range(0, 7) as $number) {
        $morning[$number] = $number;
    }
    foreach (range(8, 15) as $number) {
        $day[$number] = $number;
    }
    foreach (range(16, 23) as $number) {
        $evening[$number] = $number;
    }
    
    if(in_array($thehour,$morning)) { echo 'it\'s morning'; } // 00:00 - 07:59am
    elseif(in_array($thehour,$day)) { echo 'it\'s day'; } // 08:00 - 15:59pm
    elseif(in_array($thehour,$evening)) { echo 'it\'s evening'; } // 16:00pm - 23:59pm

    Of course if you want particular times i can change the above to suit…

    Again you’ll have to think about how to handle the timezone part yourself..

    what if I wanted to do this based on day and not time of day?

    <?php
    // Ref: http://uk3.php.net/manual/en/function.date.php
    // Mon = 1, Tues = 2, and so on...
    $thisday = date('N');
    if(isset($thisday)) {
      switch($thisday) {
        case '1':
          // Do whats here if day equals 1 (Monday)...
        break;
        case '2':
          // Do whats here if day equals 2 (Tuesday)...
        break;
    
      }
    }
    ?>

    And so on… until you have cases for 1 – 7 …

    Untested, but i see no reason it won’t work…

    It really depends what you want to accomplish. I can tailor the code to suit what you need it to do (within reason), since the above is one of several ways the same thing could be done.

    NOTE: In regard to the above, in order for date('N') to work you’ll need to be using PHP 5 (the above won’t work if you’re on less then php5 – according to the PHP website anyhow). However the code can be adjusted to use another approach.

    that gets me close but I’m still doing something wrong. I am running at last php5 btw.
    Actually what I’m trying to do is query the content of a specific post into my side bar each day using this:

    <?php query_posts(‘p=47’); if(have_posts()) : the_post(); ?>
    <?php the_content(); ?>
    <?php endif; ?>

    However using your function I can’t get it to display anythign but an error. I should add I know this is my fault and not the above function.
    Thoughts?

    Might be that you need to use a new query…

    I’ve tested this as working..

    <?php
    $thisday = date('N');
    if(isset($thisday)) {
      switch($thisday) {
        case '1':
          $dayvar = 'Monday';
        break;
        case '2':
          $dayvar = 'Tuesday';
        break;
        case '3':
          $dayvar = 'Wednesday';
        break;
        case '4':
          $dayvar = 'Thursday';
        break;
        case '5':
          $dayvar = 'Friday';
        break;
        case '6':
          $dayvar = 'Saturday';
        break;
        case '7':
          $dayvar = 'Sunday';
        break;
        default:
          $dayvar = 'Unknown day'; // This should never appear, since the value should always be equal to 1 - 7. This is here in the event it returns a value not within range of 1-7.
        break;
    
      }
    }
    // Create a new loop query
    $newquery = new WP_Query('p=162');
    // If there are results
    if($newquery->have_posts()) {
    
    // While there are results
    while($newquery->have_posts()) :
    	$newquery-> the_post();
    ?>
    	<h2><?php echo $dayvar; ?></h2>
    	<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	 at <?php the_time('g:ia') ?> on <?php the_time('M, jS') ?>
    
    <?php
    // End while loop
    endwhile;
    // Reset query after the loop - Inside the IF{} to prevent the reset causing problems with other queries that may be on the page.
    wp_reset_query();
    }
    ?>

    Of course it doesn’t do anything fancy, it just outputs the day in a head element (h2) then shows the title for the post linking to the post, and the time.

    Change p=162 to the ID of your post (that was mine while testing)..

    If you need the p= part to change depending on the day and require help, just ask… πŸ˜‰

    Or to offer an alternative, that doesn’t require PHP5 date(‘N’)…

    <?php
    $thisday = date('l');
    if(isset($thisday)) {
      switch($thisday) {
        case 'Monday':
        case 'Tuesday':
        case 'Wednesday':
        case 'Thursday':
        case 'Friday':
        case 'Saturday':
        case 'Sunday':
          $dayvar = $thisday;
        break;
        default:
          $dayvar = 'Invalid day';
        break;
      }
    }
    // Create a new loop query
    $newquery = new WP_Query('p=162');
    // If there are results
    if($newquery->have_posts()) {
    
    // While there are results
    while($newquery->have_posts()) :
    	$newquery-> the_post();
    ?>
    	<h2><?php echo $dayvar; ?></h2>
    	<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    	<br /> at <?php the_time('g:ia') ?> on <?php the_time('M, jS') ?>
    
    <?php
    // End while loop
    endwhile;
    // Reset query after the loop
    wp_reset_query();
    }
    ?>

    date('l') // That’s a lowercase L … returns the day as the actual name…

    You may find the above a nicer solution (easier to read and less lines of code).

    To explain a little….

    switch($variable) { }

    Is basically like having a series of IF’s and ELSE’s … the difference is you can specifically set which values do what, and default anything else… (so you don’t have to write IF/ELSE for every possible value).

    See how you get on… πŸ˜‰

    I’m trying to follow what you have but I think I may could explained what I’m trying to do a little clearly.
    I have 7 different posts. One for each day of the week. I want to display the corresponding post for each day of the week.
    Does that make sense or do I need to re-read what you have up there?
    By the way, thanks for your help. You are amazing.

    Ok, so where… p=X (X is a number)…

    That corresponds to 1 of 7 posts….

    You want to switch the query depending on the day.

    Example:
    It’s monday so grab post with the ID of 5..
    It’s tuesday grab the post with an ID of 19…

    etc..

    Is that correct?…

    yep…that’s it.

    Ok then, so give this a shot…

    <?php
    $thisday = date('l');
    if(isset($thisday)) {
    	switch($thisday) {
    		case 'Monday':
    			$post_to_query = 'p=1';
    		break;
    		case 'Tuesday':
    			$post_to_query = 'p=2';
    		break;
    		case 'Wednesday':
    			$post_to_query = 'p=3';
    		break;
    		case 'Thursday':
    			$post_to_query = 'p=4';
    		break;
    		case 'Friday':
    			$post_to_query = 'p=5';
    		break;
    		case 'Saturday':
    			$post_to_query = 'p=6';
    		break;
    		case 'Sunday':
    			$post_to_query = 'p=7';
    		break;
    		default:
    			// If the day is set but doesn't match the cases above
    			echo 'Something went wrong, day is not valid';
    			$errorday = strip_tags($thisday);
    			echo '<pre>'.$errorday.'</pre>';
    			$post_to_query = '';
    		break;
    	}
    } else {
    	// If the day is not set, which it should be.
    	echo 'Something went wrong, day is not valid (2) ';
    	$errorday = strip_tags($thisday);
    	echo '<pre>'.$errorday.'</pre>';
    	$post_to_query = '';
    }
    
    // Create a new loop query
    $newquery = new WP_Query($post_to_query);
    
    // If there are results
    if($newquery->have_posts()) {
    
    	// While there are results (the loop)
    	while($newquery->have_posts()) : $newquery-> the_post(); ?>
    
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    		 at <?php the_time('g:ia') ?> on <?php the_time('M, jS') ?>
    
    	<?php
    	// End while loop
    	endwhile;
    
    	// Reset query after the loop
    	wp_reset_query();
    }
    else {
    	echo 'Query for '.$post_to_query.' failed to find a valid post.';
    }
    ?>

    The messages should never appear (“Something went wrong, day is not valid”), since the day should always be equal to Monday through to Sunday, i put those messages in just incase.

    Of course update the p=1, p=2 parts, to match the ID’s of the posts..

    Hope that helps… πŸ™‚

    EDIT: Updated it a little more, so if something goes wrong it should be quite obvious by whats output to the screen.

    that’s very close, but can it actually display the content from the post in the sidebar. This looks like it just links to the post in the sidebar.
    Again, I can’t thank you enough. You are amazing.

    actually I may have got it… If I tell wordpress my week starts on Sunday will that throw things off?

    If you want the content then add it inside the loop above, i only put the title in since i assumed that’s all you wanted, but it works like the normal loop…

    This part..

    // While there are results (the loop)
    	while($newquery->have_posts()) : $newquery-> the_post(); ?>
    
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    
    		// CONTENT OR WHATEVER ELSE CAN GO HERE!!...
    		// example: the_content();
    
    		 at <?php the_time('g:ia') ?> on <?php the_time('M, jS') ?>
    
    	<?php
    	// End while loop
    	endwhile;

    Not sure if it matters whether the week starts on Monday or Sunday, shouldn’t do..

    t31os_ you’re a good man.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘rotating header images based on time of day?’ is closed to new replies.