Tutorials

Timestamps

If you’ve been reading my Build A Blog tutorial then you’ve heard me talking about timestamps. What you may be asking yourself now is, what is a timestamp, and what’s so great about it?

This is a short primer on what a timestamp is, how you get it, and some of the great things you can do with it.

A timestamp is a different sort of date format. It looks like a strange little number, but it contains a lot of information. For example:

1087076801 = Saturday June 12, 2004 04:46:41 pm

With a timestamp, you can hold all kinds of date and time information in a single number that goes in one column in a database.

There’s more than one way to get a timestamp, but I like the strtotime() function ("string to timestamp"). You can use this to get a timestamp from almost any kind of date string, like so:

strtotime("March 5, 2004");
strtotime("now");
strtotime("tomorrow");
strtotime("next Thursday");
strtotime("+1 week");
strtotime("+1 week 2 days 4 hours 2 seconds");

Obviously this function can’t take every single string you can think of. Putting in something like "last night" would error out and you’d get -1 as your timestamp. If you want a little error checking on your timestamp, try php.net’s error checking script:

<?php
$str = "Not Good";
if (($timestamp = strtotime($str)) === -1) {
   echo "The string ($str) is bogus";
} else {
   echo "$str == " . date("l dS of F Y h:i:s A", $timestamp);
}
?>

Now that you have a timestamp you might wonder what you can do with it. One great feature is that you can format dates any way you like using the date() function. Maybe your current layout is elegant, and you’d like a nice long date on your blog entries. Where $timestamp is your timestamp, you could use this to get Saturday, June 12, 2004:

date("l, F d, Y",$timestamp);

Next month, if you decide to go with a sort of chic geek layout, and you want a shorter numeric date like 06-12-04 05:03:42 try this:

date("m-d-y h:i:s",$timestamp);

For more info on formatting your dates, read php.net’s explanation of the date() function.

Timestamps are good for more than just formatting the dates, though. Suppose you want to sort your blog archives by year. Here’s how you’d get all entries from 2004:

//connect to your database
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');

// select all from the table in descending chronological order
$sql = "SELECT * FROM table ORDER BY timestamp DESC";

// select or die
$result = mysql_query($sql) or
print ("Can't select entries.<br />" . $sql . "<br />" . mysql_error());

// for each entry...
while ($row = mysql_fetch_array($result)) {
    // get the year from the timestamp
   $year = date("Y", $row['timestamp']);

   // get the date from the timestamp
  $date = date("m d Y", $row['timestamp']);

   // get the title from the database
  $title = $row['title'];

  // if the year of the entry is 2004...
  if($year == "2004") {
      // print the date and the title
      print $date . " - " . $title . "<br />";
   }
}

Understand, the above is just an example. You would have to adjust it for your database.

Here’s another example, on your index page you could display all entries from the last 24 hours:

//connect to your database
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');

// gets most recent timestamp
$sql = "SELECT timestamp FROM table ORDER BY timestamp DESC LIMIT 1";
$result = mysql_query ($sql);
list($timestamp) = mysql_fetch_row($result);

// gets all entries within 24 hours of most recent timestamp
$sql = "SELECT * FROM table WHERE timestamp >= " . ($timestamp-(60*60*24)) . " AND timestamp <= $timestamp ORDER BY timestamp DESC";
$result = mysql_query($sql);

// prints last 24 hours worth of entries
while ($row = mysql_fetch_array($result)) {
  // title and entry
  printf("<strong>%s</strong><br /><br />%s", $row['title'], $row['entry']);
}

Timestamps are easy, flexible, and have lots of uses. Hopefully you now have some ideas on how you can use timestamps to improve your website!

Comments

Error Comments are closed for this entry.