Home
  • Anger Levels
  • Contacting the Rant
  • About Rantastic
  •   Latest Posts:  ::The Rantastic :: I am a geek, in many ways…::The Rantastic :: It’s been a year.::The Rantastic :: Get Ready for an Election::The Rantastic :: Mr. Rant’s Christmas Message 2014::The Rantastic :: Scotland Decides… NO.::The Rantastic :: Guess what… It’s raining…::The Rantastic :: A Christmas Message for 2013::The Rantastic :: Birmingham Central Library Vs. Library of Birmingham::The Rantastic :: Five years, and we’re still here….::The Rantastic :: It just gets earlier every year…. 

    How to add if a post has been modified or not…

     Created at 0:16 on May 11th, 2009 by Mr. Rant. 

    There’s a very easy was to do this using PHP and a simple if/else script.  I’ll try and explain it as simply as I can.

    First off you need to write the following lines of code:

    Update Status: <?php $u_time = get_the_time(‘U’);  $u_modified_time = get_the_modified_time(‘U’);

    What were doing here is using $u_time to get the current system time from the server itself.  Next we use $u_modified_time to get the time the post was last modified, WordPress logs all page revisions and allowed you to use the different time stamps to show when they were updated last.  So now that we’ve got the time that the post was updated we can start and if/else script.

    if ($u_modified_time >= $u_time +86400) {

    It may look like a load of numbers and letters but what we’re asking the script to do is check if the modified post time ($u_modified_time) is less than (>=) the current server time ($u_time) plus 86400 seconds (+86400) which gives us twenty four hours to update a post without showing that it has been modified.  Particularly useful if you like to read back your posts after you’ve written them and you spot a mistake and you change it.  Every time you edit a post it is logged as a modified time in the database.  You can change this value if you want to, set it to any number of seconds you wish.

    Now that we’ve asked the script to do the maths for us we can tell it what we want it to do if the post has been modified.

    echo “Modified at: “; the_modified_time(‘G:i, ‘); echo (“on “); the_modified_time(‘F jS, Y’); echo(“.”); }

    We’ve just told the script to say when the post was last modified and what time it was modified. You can find out more about time stamps on the WordPress Codex.  So now we’ve told the script what to do if the post has been updated we need to tell it what to do if the post hasn’t been modified.

    else echo(“Never Updated.”); ?>

    And that’s it! You can change any of the values in quotes (” “) to anything you want.  I’ve used these because they fit with the way my site works.

    Here’s the script in it’s entirety.

    Update Status: <?php $u_time = get_the_time(‘U’);
    $u_modified_time = get_the_modified_time(‘U’); if ($u_time !== $u_modified_time) {
    echo “Modified at: “; the_modified_time(‘G:i, ‘); echo (“on “); the_modified_time(‘F jS, Y’);
    echo(“.”); } else echo(“Never Updated.”);?>

    I hope you find it useful, I certainly have.

     | Leave A Comment » Mr. Rant Written By Mr. Rant 
    Update Status: Never Updated.

    Back