Jump to content

Help Needed - Clock Displaying Date From Birth Of 'net


mehashi

Recommended Posts

Hi guys!I have been reading about basic PHP functions as I have started using it on my site. I read that there are ways to display the current date and time, as well as things like the jewish date and time for example.I would like to have a clock displaying the time (GMT) but with the date set to start from Friday October 29, 1969. (The date of the first packet exchange between UCLA and SRI).Todays date is: 10 / 06 / 2009 AD but I would like to display it like: XX / XX / XX AI (After Internet). With the first ever day being 1 / 1 / 1. There will be an irregularity in the first month as it will only have three days then it will become 1 / 2 / 1 if you understand me? (To sync the days up with a normal calendar, but the months and years relative to the birth of the net.) I will give some examples of the dates changing:1 / 1 / 1 = 29 / 10 / 19692 / 1 / 1 = 30 / 10 / 19693 / 1 / 1 = 31 / 10 / 19691 / 2 / 1 = 01 / 11 / 1969...31 / 3 / 1 = 31 / 12 / 1969 1 / 4 / 1 = 1 / 1 / 1970...30 / 12 / 1 = 30 / 09 / 1970 1 / 1 / 2 = 01 / 10 / 1970...10 / 9 / 40 AI = 10 / 06 / 2009 AD (The 10th day of the 9th month of the 40th year of the use of the internet.)I am sorry if that is confusing, hopefully you understand. Anyway the thing I was wondering is if PHP is capable of this, maybe a simple formula I could use to generate and display the date in this format? I am still new to PHP but I can learn pretty quickly! Any help would be greatly appreciated as I would really like to implement this in my site!Thanks guys! ^_^b

Link to comment
Share on other sites

PHP has support for the Unix epoch timestamp, which has a baseline of 1/1/70. The easiest way will probably be to use the standard built-in time support for that, and add support for previous dates. Unix timestamps are given in seconds, so a value of 0 actually corresponds to 12/31/1969 23:59:59, a value of 1 is 1/1/1970 00:00:00. So what you need is to basically add support for negative timestamps that can go back to October 69. It's probably easiest to keep everything in seconds, and do some math to figure out how many months and days correspond to a certain number of seconds.I'm trying to think about how I would do this. I would probably start with the Unix timestamp. I guess you really need two functions, like PHP's mktime and getdate functions. One function needs to take a certain date and time in your format and return the number of seconds since the start date. The other needs to take the timestamp and return information about how many months and years that represents. In terms of converting between calendars, you can convert a standard date to its Unix timestamp, then just add however many seconds are between your start date and 1/1/70, and that will be the timestamp for your calendar, which you can use one of the functions to convert to date information. So converting between calendars is the easy part, the difficult part is taking a timestamp and figuring out which date it corresponds to.The easiest way to do that is probably to just start drawing it out. There are 86400 seconds in a day. So let's see:

0-86399: m=1;d=1;y=186400-172799: m=1;d=2;y=1172800-259199: m=1;d=3;y=1259200-345599: m=2;d=1;y=1345600-431999: m=2;d=2;y=1432000-518399: m=2;d=3;y=1518400-604799: m=2;d=4;y=1

The shorter length of the first year might be an issue, it's missing 28 days compared to every other year. You can divide the timestamp by 86400 to get the number of days, and then go from there. I'm sort of just writing things down as I think here.Wait, there's a problem: what about leap years? Which month does an extra day get added to? More importantly, how do you determine which year is a leap year? Would you use the same algorithm for gregorian calendars?

Link to comment
Share on other sites

Hi Justsomeguy, thank you!I am glad it is not just me that realised the complexity of a new time system!This has been a point of discussion between myself and friends for a while now. We all have our own ideas how this would work correctly. My idea is something like this:day (same as gregorian, as the 10th is still the 10th no matter which month)month (contain same days, but start from October - so the 4th month contains 28/29 days - allowing leap years to not screw with the system)year (Starting from 1969)So one could say that today is the 12th day of the 9th month of the 40th year of the use of the internet. phew...If you can think of a better system let me know, like I said we all have our own best idea!Im looking at your unix data. If the calendar starts 1 / 1 / 70, that leaves -64 days to account for. If 64 x 86400 = 5529600 then could I maybe make a request for a unix timestamp >> add 5529600 >> then convert it to the calendar date in my format? Please forgive me I am very new to PHP and just guessing here! Or is it a matter of having to process the negative value instead?What about the cheating option of (day=d) / (month=m -9) / (year=y -1968) to convert from gregorian to my "use of internet" calendar with the -9 of the months set to roll to the previous year if month is <10? Maybe to repeat the list of months twice to ensure it doesn't fall outside of a variable. That would seem like a way to do it maybe, although a little clumsy?

The shorter length of the first year might be an issue, it's missing 28 days compared to every other year. You can divide the timestamp by 86400 to get the number of days, and then go from there. I'm sort of just writing things down as I think here.
Do you mean the first unix year? I think I will have to do some googling when I get home! (at work at the moment)Thanks for your input, you have already helped focus my mind on the issues! ^_^bI will try writing a basic piece of code when I get home!
Link to comment
Share on other sites

I like this idea:

Im looking at your unix data. If the calendar starts 1 / 1 / 70, that leaves -64 days to account for. If 64 x 86400 = 5529600 then could I maybe make a request for a unix timestamp >> add 5529600 >> then convert it to the calendar date in my format? Please forgive me I am very new to PHP and just guessing here! Or is it a matter of having to process the negative value instead?
That sounds about right. Really the only difficulty is taking a timestamp in your format and converting it to the date information. Frankly, probably the best way to figure that out would be to try and find the source code to PHP's getdate function. It would be written in C, but at least you'd be able to see the algorithm they're using to convert from Unix timestamp to Gregorian calendar. You can find the source code for PHP here:http://www.php.net/downloads.phpI'm at work right now so I'm not able to download that and go through it (would take too long, it's only a half-day for me on Fridays), but I'll check it out this afternoon when I get home. I'm not sure of the file structure, but if you extract that zip download I'm sure it will contain a ton of .c and .h source files. The date functions should be living somewhere near the core code.I might be over-thinking this though, maybe all you really need to do is convert the timestamp using getdate, and then just subtract like you were talking about. I'm thinking there are going to be complications there when the day falls on a month boundary, you'll need to be able to figure out if it's the last day or first day of a month.Incidentally, is that a picture of you? That looks a lot like an incredibly cute girl I know in Australia.
Link to comment
Share on other sites

Incidentally, is that a picture of you? That looks a lot like an incredibly cute girl I know in Australia.
*giggles gently in the background*
Link to comment
Share on other sites

Unfortunately it's not as commented as I was hoping, but it looks like this function does most of the work of converting a Unix timestamp to the date information:

/* Converts a Unix timestamp value into broken down time, in GMT */void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts){	timelib_sll days, remainder, tmp_days;	timelib_sll cur_year = 1970;	timelib_sll i;	timelib_sll hours, minutes, seconds;	int *months;	days = ts / SECS_PER_DAY;	remainder = ts - (days * SECS_PER_DAY);	if (ts < 0 && remainder == 0) {		days++;		remainder -= SECS_PER_DAY;	}	DEBUG(printf("days=%lld, rem=%lld\n", days, remainder););	if (ts >= 0) {		tmp_days = days + 1;		while (tmp_days >= DAYS_PER_LYEAR) {			cur_year++;			if (timelib_is_leap(cur_year)) {				tmp_days -= DAYS_PER_LYEAR;			} else {				tmp_days -= DAYS_PER_YEAR;			}		}	} else {		tmp_days = days;		/* Guess why this might be for, it has to do with a pope;-). It's also		 * only valid for Great Brittain and it's colonies. It needs fixing for		 * other locales. *sigh*, why is this crap so complex! */		/*		if (ts <= TIMELIB_LL_CONST(-6857352000)) {			tmp_days -= 11;		}		*/		while (tmp_days <= 0) {			if (tmp_days < -1460970) {				cur_year -= 4000;				DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););				tmp_days += 1460970;			} else {				cur_year--;				DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););				if (timelib_is_leap(cur_year)) {					tmp_days += DAYS_PER_LYEAR;				} else {					tmp_days += DAYS_PER_YEAR;				}			}		}		remainder += SECS_PER_DAY;	}	DEBUG(printf("tmp_days=%lld, year=%lld\n", tmp_days, cur_year););	months = timelib_is_leap(cur_year) ? month_tab_leap : month_tab;	if (timelib_is_leap(cur_year) && cur_year < 1970) {		tmp_days--;	}	i = 11;	while (i > 0) {		DEBUG(printf("month=%lld (%d)\n", i, months[i]););		if (tmp_days > months[i]) {			break;		}		i--;	}	DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, cur_year, i + 1, tmp_days - months[i]););	/* That was the date, now we do the tiiiime */	hours = remainder / 3600;	minutes = (remainder - hours * 3600) / 60;	seconds = remainder % 60;	DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););	tm->y = cur_year;	tm->m = i + 1;	tm->d = tmp_days - months[i];	tm->h = hours;	tm->i = minutes;	tm->s = seconds;	tm->z = 0;	tm->dst = 0;	tm->sse = ts;	tm->sse_uptodate = 1;	tm->tim_uptodate = 1;	tm->is_localtime = 0;}

I'll need to go through that and figure out what's going on, that's defined in /ext/date/lib/unixtime2tm.c.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...