Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hundred year date conversion

Status
Not open for further replies.

RPGguy

Programmer
Jul 27, 2001
73
0
0
US
I have to search a database where all dates are stored in 'hundred year' format (10/27/2007 = 39380). I've looked all over but have not seen a function or any PHP code to do this. Thanks in advance for any help.
 
Code:
function convertToDate($HYD){
  return date("j/n/Y", strtotime("+$HYD days", strtotime("1900-01-01 00:00:00")));
}
 
Thanks JPADIE I was hoping you would answer this. Since I have to convert the dates both ways could you show me a function that had one parm for conversion type and one for the date?

function convertd(1,10-26-2007) returning (39380)
function convertd(2,39380) returning (10/26/2007)

Thanks again as always.
 
Code:
function converttoHYD($date){
 $start = strtotime('1970-01-00 00:00:00');
 $current = strtotime($date);
 $delta = ($current-$start); //delta in seconds
 //calculate number of days
 $days = $delta/(60*24);
 return floor($days); //just return the number of days (no fraction)
}

not tested ...
 
Something is off with the math. Today's date in my system is 39380 so my starting date needs to be 1900-01-01 (39380/365=107.89).

When I use that date to calc $start my result is 13812.
If I make the starting year 1903 my result is 38284 which is on the right track. Could it be a data overflow issue?
 
my fault. there is a float limitation in strtotime which with modern versions of php take you back only to 1901. an important missing link for your purposes.

the secret is therefore to use julian dates.

Code:
function convertToHYD($date){
	//takes a date like yyyy-mm-dd
	list ($year, $month, $day) = explode("-", $date);
	$jd  = gregoriantojd($month, $day, $year);
	$sd = gregoriantojd(1,1,1900);
	return ($jd - $sd);
}

function convertFromHYD($date){
	$sd = gregoriantojd(1,1,1900);
	$total = $sd + $date;
	return jdtogregorian($total);
}
echo ($hyd = converttohyd('2007-10-27'));
echo "<br/>";
echo convertfromhyd($hyd);

outputs

Code:
39380
10/27/2007
as expected

I am surprised that you got 36380 for yesterday's date. it may be that your function normalises everything to GMT whereas the above do not. if you need this, you will need to adjust the above functions for your timezone offset. personally, I am not sure what the rules of the hundredyear date storage method are (i've been surmising that it's the number of days since midnight on 1/1/1900)
 
Once again, you are the man. Thanks so much for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top