Jetpack Infinite Scroll

Just wondering if you have noticed lately. I have implemented WordPress Jetpack plugin’s Infinite Scroll. So, no more pagination. Just endless scroll down. In fact, you can scroll all 1628 posts if you want. I’ve tried other infinite scroll plugins, but none of them work, but Jetpack worked out of the gate. I did have to make some modifications to the footer section. Due to the nature of infinite scroll, you’ll never get to see your original footer, unless you reached the last post, which could take a while depending on the number of posts you have. Jetpack gives you this generic footer which I ended up editing by adding this code in my functions.php file.

/**
 * Customized Jetpack Infinite Scroll.
 */
add_filter( 'infinite_scroll_credit', 'urr_my_footer_text' );
function urr_my_footer_text() {
	$this_year = date('Y');
	$footer_text = 'Copyright © 2003-'.$this_year.'. All rights reserved';
	return $footer_text;
}

The code above resides in functions.php. It inserts a customized footer containing the copyright notice which spans several years and displays the current year using the PHP date function. In addition, I also had to uncomment the underscore pagination from several templates, namely archive.php and search.php so that the navigation links don’t show up in the middle of your infinite scroll page.

<?php //framework_numeric_posts_nav(); ?>
<?php //framework_paging_nav(); ?>

Calculating Dates in PHP

Displaying dates in PHP is a no brainer. You simply echo the date() function to display the current date. For example, echo date(‘Y-m-d’) will display the current date in the ISO date format of ‘2012-02-27.’ However, calculating dates in the past or in the future is a tiny bit more tricky. For accuracy, I recommend that you use mktime(), which converts time to Unix timestamp. You can then perform date calculations in Unix timestamp which is much more accurate. You do have the option to change the format back to ISO, if you desire. Here are a few examples.

Display the current date:

echo date('Y-m-d');

Output: 2012-02-27

Convert to mktime:

$t = mktime(0, 0, 0, date("m"), date("j"), date("Y"))

Display the start of the current month:

echo date('Y-m-d', mktime(0, 0, 0, date("m"), 1, date("Y")));

Output: 2012-02-01

Display the end of the current month:

echo date('Y-m-d', mktime(23, 59, 59, date("m")+1, 0, date("Y")));

Output: 2012-02-29

Display the start of last month:

echo date('Y-m-d', mktime(0, 0, 0, date("m")-1, 1, date("Y")));

Output: 2012-01-01

To display the end of last month:

echo date('Y-m-d', mktime(23, 59, 59, date("m"), 0, date("Y")));

Output: 2012-01-31

Display the start of next month:

echo date('Y-m-d', mktime(0, 0, 0, date("m")+1, 1, date("Y")));

Output: 2012-03-01

Display the end of next month:

echo date('Y-m-d', mktime(0, 0, 0, date("m")+2, 0, date("Y")));

Output: 2012-03-31

This code is very flexible. If you like to display the dates to any other format, you can change the date format to any format that you like. Check out all the different date formats. I hope this was helpful.

Make Your Copyright Notice Dynamic

Most website and blog owners place some kind of copyright information on their websites and in their blogs. Copyright notices mean that the copyright holder, reserves all the rights provided by copyright law, such as distribution, performance, and creation of derivative works.

Copyrights are usually found in the footer of most designs, themes or templates. My copyright information is found in the footer. It says, “Copyright © 2003-2011. All rights reserved.” The copyright notice will need to be updated yearly to reflect the current year.

Some owners place a range of years, such as 2000-2010, just like mine, to reflect that the website or blog has been around for a number of years. Often times, the owner forgets to update the copyright information. It’s a simple text change, but there are ways to make it dynamic.

By making your copyright notice dynamic means you never have to edit the copyright notice ever again. At every New Year’s eve, while you are out celebrating, your website magically updates the copyright notice to the latest year. How? It uses a PHP function called date().

To get the current year, use the PHP date function:

<? echo date(“Y”); ?>

If you want a range of years, you can use:

2001-<? echo date(“Y”); ?>

There you have it, a dynamic copyright notice! And one more thing, to display the copyright symbol properly, use the HTML special symbol:

&copy; or &#169; which are the same as the symbol ©

PHP Time of Day Script

One very cool way of taking advantage of PHP’s powerful language is to make a determination as to what time of the day it is, and making a decision based on that information. For example, we want to detect the server’s current time and display a message that says either good morning, good afternoon or good evening.

There’s a handy PHP function called date that we can use. We can assign a variable called $current_time, to the server’s current time.

$current_time = date(G);

Next, let’s display the logic.

if ($current_time >= 0 && $current_time < 12) { echo 'Good Morning';}
if ($current_time >= 12 && $current_time < 18) { echo 'Good Afternoon';}
if ($current_time >= 18 && $current_time < 24) { echo 'Good Evening';}

You can also display images based on time of day.

if ($current_time >= 18 && $current_time < 24) { echo<img src=”sample.jpg” />;}