Written by: NetworkError, on 07042009 10:25
Last update: 07042009 13:34
Published in: Public,
Technical Wootness
Views: 8044
How long have you been slaving away at your job? Married? Living a lie? Whatever it is, finding out how long it's been going on is interesting and watching the seconds tick away is even more interesting.
Or do you want a countdown timer to a special date?
In any case, I have the solution. Without further adieu, I give you the "Years of Service" or "Count Up/Down" clock (JavaScript Edition). To use, fill out the simple form below and bonk on the button.
Years of Service Clock:
0
years (decimal)

0

years 
0

/ 
52 
weeks 
0

/ 
7 
days 
0

/ 
24 
hours 
0

/ 
60 
minutes 
0

/ 
60 
seconds 


PHP Command Line Version
If you want to run this in a terminal somewhere, try the PHP version...
(Note: I know this could be optimized, I just haven't had the time to do it.)
#! /usr/bin/php
<?
/**
* Some "contants" for configuring the script.
**/
// How many decimal points do you want in your decimal year counter?
$ROUND_DECIMAL_YEAR = 7;
// What is your start date (and time, optionally).
$START_DATE = '08/02/2004 9:00 am';
// What "title" do you want displayed on the first line of the script?
echo 'Employed for:'."\n";
// Convert start date to Unix timestamp.
$start_date = strtotime($START_DATE);
do {
// Current date/time  start date/time
$duration = time()  $start_date;
// Do a bunch of division to get all the pieces of "how long you have worked here" figured out.
$years_decimal = round(($duration / (365 * 24 * 60 * 60)), $ROUND_DECIMAL_YEAR);
// Add zeros if necessary to pad the decimal year to the desired length.
if ($ROUND_DECIMAL_YEAR > 0) {
if (($pos = strpos($years_decimal, '.')) === false) {
$years_decimal .= '.';
$fill_length = $ROUND_DECIMAL_YEAR;
} else {
$decimal_points = strlen(substr($years_decimal, ($pos + 1)));
$fill_length = $ROUND_DECIMAL_YEAR  $decimal_points;
}
if ($fill_length != 0) {
for ($i = 0; $i < $fill_length; $i++) {
$years_decimal .= '0';
}
}
}
$years = (int)($duration / (365 * 24 * 60 * 60));
$remainder = $duration % (365 * 24 * 60 * 60);
/* Use weeks instead. Months aren't constant enough.
$months = (int)($remainder / (30.4375 * 24 * 60 * 60));
$remainder = $remainder % (30 * 24 * 60 * 60);
*/
$weeks = (int)($remainder / (7 * 24 * 60 * 60));
$remainder = $remainder % (7 * 24 * 60 * 60);
$days = (int)($remainder / (24 * 60 * 60));
$remainder = $remainder % (24 * 60 * 60);
$hours = (int)($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = (int)($remainder / 60);
$remainder = $remainder % 60;
$seconds = $remainder;
// Assemble the "how long you have worked here" line.
$duration = '('.$years_decimal.' years) '."$years years $weeks/52 weeks ".
"$days/7 days $hours/24 hours $minutes/60 minutes $seconds/60 seconds ";
// Echo the duration.
echo $duration;
// Wait 1 second before updating.
sleep(1);
// Backspace over the string we just output so we can rewrite it.
for ($i = 0, $length = strlen($duration); $i < $length; $i++) {
echo chr(8);
}
} while (true); // Loop until killed.