Archive

Archive for February, 2009

Convert A Date Into Timestamp In JavaScript

February 27th, 2009 1 comment

I have previously talked about using PHP to convert from a date to a time, but what about doing the same in JavaScript?

To get the unix timestamp using JavaScript you need to use the getTime() function of the build in Date object. As this returns the number of milliseconds then we must divide the number by 1000 and round it in order to get the timestamp in seconds.

Math.round(new Date().getTime()/1000);

To convert a date into a timestamp we need to use the UTC() function of the Date object. This function takes 3 required parameters and 4 optional parameters. The 3 required parameters are the year, month and day, in that order. The optional 4 parameters are the hours, minutes, seconds and milliseconds of the time, in that order.

To create the time do something like this:

var datum = new Date(Date.UTC('2009','01','13','23','31','30'));
return datum.getTime()/1000;

This prints out 1234567890. Here is a function to simplify things:

function toTimestamp(year,month,day,hour,minute,second){
 var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
 return datum.getTime()/1000;
}

Notice that when adding in the month you need to minus the value by 1. This is because the function requires a month value between 0 and 11. However, there is an easier way of getting the timestamp by using the parse() function. This function returns the timestamp in milliseconds, so we need to divide it by 1000 in order to get the timestamp.

function toTimestamp(strDate){
 var datum = Date.parse(strDate);
 return datum/1000;
}

This can be run by using the following, not that the date must be month/day when writing the date like this.

alert(toTimestamp('02/13/2009 23:31:30'));

Or even this:

alert(toTimestamp('2009 02 13 23:31:30'));

Displaying WordPress Authors

February 26th, 2009 2 comments

WordPress has a couple of rarely used functions that allow author information to be displayed for the current post and a list of all of the authors on the blog.

Adding a written by message to your posts is not difficult at all. Just use the the_author_posts_link() function inside the post loop.

<?php the_author_posts_link(); ?>

This function shouldn’t be confused with the the_author_link() function that prints out the link in the author’s profile.

The second way of printing out author information is by using the wp_list_authors() function. This can take a number of arguments, but the simplest use of it is as follows:

<ul>
<?php wp_list_authors(); ?>
</ul>

This should print out a list of the authors on your blog. There is one very important thing I should mention here. This function will only print out an authors name if they have written a post and are not the default admin account. I tried to print out the authors on a test blog and after a few minutes of frustration I looked at the function call itself on line 460 of the file author-template.php in the directory wp-includes. The function specifically excludes authors that haven’t written any posts but you can turn this on if you give it the right parameter.

The wp_list_authors() function takes a number of arguments in the form of a string of arguments. The available arguments are as follows:

  • optioncount: Print out the number of posts for each author. The default is 0 (false).
  • exclude_admin: Print out the blog admin user. The default is 1 (true)
  • show_fullname: Use the full name of the author instead of their nickname. Default is 0 (false).
  • hide_empty: Show users who don’t have any posts. The default is 1 (true). Note that even if a user with no posts is printed they will not be linked.
  • echo: Immediately echo the list of authors or return as a variable. The default is 1 (true).
  • feed: If set then a link to an RSS feed for that author is printed, along with the text given. The default is not to print anything.
  • feed_image: This is a link to a RSS icon, it works in the same way as the feed parameter, but will override it.

To use the arguments you need to pass them to the function in the form of a string or arguments like this.

<?php wp_list_authors('exclude_admin=0&hide_empty=0&feed=Feed'); ?>

Now that you have a link of authors being printed out it is time to look at where the links go to. Each author has an ID associated with them, and this is appended to the URL using the author tag. If you have permalinks turned on then the authors name is used.

When clicked on WordPress will attempt to display the author information using a template file called author.php. If this is not present then it will display a list of the posts using the archive.php template file. Finally, if this is not present then WordPress will default to the index file.

Your author.php file should contain the following lines so that you can access the author informaiton.

<?php
if(isset($_GET['author_name'])) :
 $curauth = get_userdatabylogin($author_name);
else :
 $curauth = get_userdata(intval($author));
endif;
?>

The $curauth variable now contains an author object that can be used to access the various different data fields associated with authors. The following snippet will print out the authors name, profile and homepage, if this information is not filled in then nothing will be printed.

<h2><?php echo $curauth->first_name . ' ' . $curauth->last_name; ?> (<a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a>)</h2>
<p><?php echo $curauth->user_description; ?></p>

You can also include a normal loop on this page to print out every post that the user has written.

For more information on the author templates (and a full list on the attributes available in the author object) take a look at the author templates section on the WordPress site.

Find Appropriate Colour In PHP

February 25th, 2009 No comments

Use the following function if you want to print the text in a particular colour, depending on what the background colour is.

function opposite($color) {
 $white = 0;
 for ($i = 0; $i < 6; $i += 2) {
  $white += base_convert(substr($color,$i,2),16,10) > 127 ? 1 : 0;
 }
 return $white >= 2 ? "000000" : "ffffff";
}

It works by looking at the colour and seeing how much of it is white. If greater than two parts are significantly white then the color returned is black, otherwise white is returned.

Here are some examples to test the function out.

echo '<span style="background-color:#ffffff;color:#'.opposite('ffffff').'">XXXXXX</span>';
echo '<span style="background-color:#000000;color:#'.opposite('000000').'">XXXXXX</span>';
echo '<span style="background-color:#fedbad;color:#'.opposite('fedbad').'">XXXXXX</span>';
echo '<span style="background-color:#123456;color:#'.opposite('123456').'">XXXXXX</span>';
echo '<span style="background-color:#fe13ef;color:#'.opposite('fe13ef').'">XXXXXX</span>';

Turn Off WordPress Revisions

February 24th, 2009 1 comment

WordPress has a nice little revisions feature that will allow you to revert to a previous version of a post if you don’t like the current edit. However, the drawback of this feature is that it is not always needed and it fills the post table full of stuff you will never need. Fortunately, turning this feature off isn’t too much of a pain. All you need to do is add the following line of code to your wp-config file, just below the DB_COLLATE line.

define('WP_POST_REVISIONS', 0);

You can also set the autosave interval here to something greater than the default of 60 seconds. It is possible to do this in the wp-config file since version 2.5.0.
define('AUTOSAVE_INTERVAL', 123);

If you want to get rid off all of the post revisions from your post table then you can use the following SQL query.

DELETE FROM wp_posts WHERE post_type = 'revision';

Find File Extension In PHP

February 23rd, 2009 1 comment

This simple code example uses a combination of strrchr to find the last occurrence of a string and substr to return part of the string in order to find the file extension for a given filename. This is ideal if you want to quickly find a file extension.

$ext = substr(strrchr($fileName, '.'), 1);

This code can be used in the following way.

$fileName = '\path\to\file\afile.wibble';
$ext = substr(strrchr($fileName, '.'), 1);
echo $ext;

The output here is 'wibble';