I have a blog and would like to add a "Random Post" button.
I'm hosted with a company called Blogware which, unlike Blogger and WordPress, seems to have
no no way to add a "random post" button.
I'm not a programmer but suspect this should be fairly easy to do. I'd like advice on how to go about this - do I try to do it in JavaScript? PHP? Something else? Whatever's the easiest solution is best for me. (Major bonus points if someone provides the code itself that would do this, either as a text link or a button I can add.)
All posts on my blog have the basic format of: http://blog.jason.hammond.net/blog/_archives/yyyy/m/dd/
...so if I can define the start date of my blog (Feb 28, 2006) and occasionally update the script with the most recent date of a post as an end point, that would be close enough for what I want. (I try to post daily but don't mind if users end up on a blank day or don't have every single recent post I've made in the range.)
I should also mention that I'm not able to get to the "guts" of the site but can add third-party widgets and other bits of code.
To do this, I think I need to:
- define the start date of the range (2006/2/26)
- define the end date (for now, say 2008/4/26) and be able to easily change this monthly or whenever I get a chance
- generate a random date in the format yyyy/m/dd
- append that to the URL: "http://blog.jason.hammond.net/blog/_archives/"
- go to that URL
Thanks very much in advance for your help!
$startdate = mktime(0,0,0,2,26,2006);
$enddate = mktime(0,0,0,4,26,2008);
$daysbetween = floor(($enddate - $startdate) / (60 * 60 * 24));
$targetdate = $startdate + (rand(0,$daysbetween) * 60 * 60 * 24);
header("Location: http://blog.jason.hammond.net/blog/_archives/" . date("Y/n/j", $targetdate);
exit();
I didn't test it so your mileage may vary. Give it a shot?
posted by bertrandom at 9:38 AM on April 27, 2008