Title: Help needed with php within php code
Last modified: August 20, 2016

---

# Help needed with php within php code

 *  Resolved [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/)
 * Hi folks,
 * Im trying to write some code that contains php within php. Whatever i do seems
   to result in an error so can anyone help. basically it is a conditional statement
   that asks wordpress if this is a certain category then to echo something.
 *     ```
       <h1>
          <?php
          if ( is_category('new-this-week') ) {
       					echo 'New This Week';
       					} elseif ( is_category('new-this-month') ) {
       					echo '<?php the_time('F, Y') ?>';
       					} else {
       					}
           ?>
         </h1>
       ```
   
 * Thanks to anyone that can help

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409577)
 * Try changing this `echo '<?php the_time('F, Y') ?>';` to this `echo the_time('
   F, Y');`
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409580)
 * Fantastic, works perfectly! Thanks for that.
 * I don’t suppose you know if its possible for WordPress to echo a date range? 
   For example, rather than have the above code echo ‘new this week’, is there code
   that will enable it to echo ‘posts from 10-17 November’ and then on the next 
   day automatically change to echo ‘posts from 11-18 November’ and so on?
 * Thanks for your help.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409582)
 * Did you mean, only show Posts from 10-17 November?
    Look at the time parameters
   of WP_Query. [http://codex.wordpress.org/Function_Reference/WP_Query#Time_Parameters](http://codex.wordpress.org/Function_Reference/WP_Query#Time_Parameters)
 *  [Knut Sparhell](https://wordpress.org/support/users/knutsp/)
 * (@knutsp)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409584)
 * There is a PHP inconsistency in your code. Either you’re IN PHP script mode or
   your are outside (HTML mode). Change `echo '<?php the_time('F, Y') ?>';` to just`
   the_time('F, Y');`
 * [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/):
    The function`
   the_time()` will output the time of the post. Nothing to `echo` in this case.
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409586)
 * [@knut](https://wordpress.org/support/users/knut/) Sparhell
 * So i don’t need the echo, just `the_time('F, Y');` ? It appears to work either
   way.
 * [@keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * No i don’t want it to display posts from a date range as i already have that 
   in the code that follows. Its just an h1 tag on the sidebar and i would like 
   it to automatically echo the time period that the posts are showing, which in
   this case is for the last 7 days. So if today was November 17th it would simply
   echo ‘posts from November 10-17’. Then tomorrow it would echo ‘posts from November
   11-18’ and so on. Is this possible?
 * Thanks to both of you for your help.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409589)
 * Try it with this:
 *     ```
       <?php
       echo date('d'); // today
       echo date('d', strtotime('-1 week')); // today minus 7 days
       ?>
       ```
   
 * [http://php.net/manual/en/function.date.php](http://php.net/manual/en/function.date.php)
   
   [http://www.the-art-of-web.com/php/strtotime/](http://www.the-art-of-web.com/php/strtotime/)
 *  [Knut Sparhell](https://wordpress.org/support/users/knutsp/)
 * (@knutsp)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409590)
 * `echo the_time()` will output exactly the same as `the_time()`
 * Point is, you have an extra `<?php` inside a php block. This will give an error.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409594)
 * something like this:
 *     ```
       <?php
       $this_month = date('F');
       $this_month_minus_seven_days = date('F', strtotime('-1 week'));
       // check if today minus 7 days is the same month
       if($this_month != $this_month_minus_seven_days) {
       echo 'posts from '. date('F d', strtotime('-1 week')).' - '. date('F d');
       } else {
       echo 'posts from '. date('F d', strtotime('-1 week')).' - '.date('d');
       }
       ?>
       ```
   
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409610)
 * Awesome!
 * Much appreciated.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409614)
 * You’re welcome. Glad you got it resolved.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Help needed with php within php code’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/help-needed-with-php-within-php-code/#post-2409614)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
