Some morbid ColdFusion Birthday fun

This post is more than 2 years old.

So, many, many, many years ago I built a little web app called the Death Clock. I wrote it in Perl (which is what I began my web dev career in) and dropped it on my free university web space and promptly got kicked off in a week when it "spiked" at about 1000 hits per day. (This was back when web counters were big. I can't describe the excitement I got from reloading my page and seeing the little graphical counter - also a Perl script - incrementing by 5-10 hits.) I rewrote the code in pure JavaScript and then - a few years later - rewrote the site in ColdFusion. I sold that web site close to 6 or 7 years ago, but the code behind it is simple - and fun - so I thought I'd share a quick example today.

First off - some background. The idea behind the Death Clock was simple. Based on your birthday, and your gender, you can make a rough guess (and trust me, a lot of people simply didn't get that it was for fun) at when you would die. That by itself isn't that interesting. But I used a bit of code to convert into seconds and then count down from there. Here is a quick example of what I mean.

<cfset bday = createDate(1973,4,8)> <cfset lifeSpan = 72>

<cfset deathDay = dateAdd("yyyy",lifeSpan,bday)>

<cfoutput> Your day of death is #dateFormat(deathDay, "full")#. <p/> </cfoutput>

<cfset timeLeft = dateDiff("s",now(),deathDay)>

<cfoutput> You have #numberFormat(timeLeft)# seconds left to live. </cfoutput>

So given a birthday of - today (yep, that's me :) and a lifespan of 72 years (for men, and that's probably not accurate now), we can calculate a "death day" by simply using date add. For me that is Saturday, April 8, 2045, which means I'll be able to get a full week of work in at least before I kick the bucket.

The next part simply calculates the difference in time - by seconds - between now and then. What's interesting is that back when I first wrote the Death Clock in ColdFusion, the dateDiff function had a small integer maximum. I kept getting the wrong results until I realized it was actually "flipping" over. I switched to getting days and then just multiplied out.

Of course, I couldn't just leave it at that without adding a bit of jQuery. In this example I've put up two examples, and added some jQuery to grab the values and decrement them. The addCommas function is courtesy of this blog entry.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(document).ready(function() { var foundDoms = $(".numberCounter") if(foundDoms.length > 0) setInterval("updateNumbers()", 1000) })

function updateNumbers() { $(".numberCounter").each(function() { var currentVal = $(this).text() //remove commas from the # currentVal = currentVal.replace(/,/g,"") if(currentVal > 0) currentVal-- currentVal = addCommas(currentVal) $(this).text(currentVal) }) }

function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } </script>

<cfset bday = createDate(1973,4,8)> <cfset lifeSpan = 72>

<cfset deathDay = dateAdd("yyyy",lifeSpan,bday)>

<cfoutput> Your day of death is #dateFormat(deathDay, "full")#. <p/> </cfoutput>

<cfset timeLeft = dateDiff("s",now(),deathDay)>

<cfoutput> You have <span class="numberCounter">#numberFormat(timeLeft)#</span> seconds left to live. </cfoutput>

<hr/>

<cfset bday = createDate(2010,4,8)> <cfset lifeSpan = 72>

<cfset deathDay = dateAdd("yyyy",lifeSpan,bday)>

<cfoutput> Your day of death is #dateFormat(deathDay, "full")#. <p/> </cfoutput>

<cfset timeLeft = dateDiff("s",now(),deathDay)>

<cfoutput> You have <span class="numberCounter">#numberFormat(timeLeft)#</span> seconds left to live. </cfoutput>

Completely pointless, I know, but if you want to try it yourself, visit here:

http://www.coldfusionjedi.com/demos/apr82010/test4b.cfm?date=4/8/1973

Tweak the URL to enter your own date (and I spent 2 seconds adding validation so I'm sure it can break pretty easily) and sit back. Time is fleeting. ;)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by SuperAlly posted on 4/8/2010 at 9:51 PM

Well thanks for ruining the surprise Ray!

Besides, I think your formula is faulty because my wife has me scheduled to clock out MUCH earlier than that :)

Comment 2 by Jim Papaleo posted on 4/8/2010 at 9:55 PM

Nice Ray, for a old timer like me, I've only got 11 years left! Actually, I'm planning on hitting 90.

Comment 3 by Phillip Senn posted on 4/8/2010 at 10:10 PM

Gives a whole new meaning to the phrase "your session has ended".
This could be used to promote the new movie: Repo Men.

Comment 4 by Craig Inman posted on 4/8/2010 at 11:32 PM

...happy birthday old man!

Comment 5 by Jason Fisher posted on 4/9/2010 at 4:40 AM

I always forget that you wrote that original, Ray, though I know it has come up before. Our pastor likes to put the counter up on screen every so often with his personal countdown running while he speaks, and it can be a lot of fun. "The statistics on death are very compelling: every 1 out of 1 people will die at some point."

Happy Birthday, and thanks for the fun little bits intermingled with the serious CF stuff over the years :)

Comment 6 by Prasanth Kumar.S posted on 4/9/2010 at 8:02 AM

Many many happy returns of the day, Ray.

Comment 7 by Brian W. posted on 4/9/2010 at 10:54 PM

I had no idea you were behind "The Death Clock". I may very well have been on of those ticks on your counter. :)

As always - thanks for sharing!

Comment 8 by Andy Sandefer posted on 4/9/2010 at 10:54 PM

This is bullshit. First off, I've lost 52 lbs over the last year and the deathclock's BMI calculator still says that I'm obese! I'm 5'11'' and I weigh 220 (all muscle ;0) - so yeah I've still got a gut and I still smoke a bit but I also play tennis, lift, etc. and I even ran a competitive 5 mile trail run (up and down big hills, through streams, loose dirt - tricky stuff) last month and I completed the trail in under an hour.

I quit eating fast food everyday because it is poisonous and I make sure to engage in some type of exercise EVERY day. I also switched to 64 calorie beer (aka beer water).

So why then must I only live until 65? Jeez, I'm 35 now and I've got a lot more left in the tank - "Things are gonna change I can feel it". Anyway @Ray the deathclock reaper guy - I guess you'll be down one reader who actually buys stuff on your Amazon list come the year 2040.

Comment 9 by Raymond Camden posted on 4/9/2010 at 11:00 PM

@Andy: Heh, this is EXACTLY the kind of email I used to get every day at the Death Clock. ;) (And most were serious too - which is the scary part.)

Comment 10 by Andy Sandefer posted on 4/9/2010 at 11:18 PM

I am serious @Ray - and I've already dispatched my goons to Louisiana to get you to rewrite the death clock to favor my health conditions and give me a more optimistic prognosis.

Just kidding, how was your birthday - you really need to watch the South Park about Facebook and TRON - it was on again last night and it is one of the best episodes they've every written. It's titled something to the effect of "You have 0 friends".

Ciao!

Comment 11 by Raymond Camden posted on 4/9/2010 at 11:21 PM

I saw a 2 minute cut from the show - looked awesome.

Comment 12 by Late commenter posted on 4/14/2010 at 1:46 AM

The higher I make my BMI, the longer I get to live? That doesn't seem right. I'm going out for a steak dinner RIGHT NOW, thanks!