Page 1 of 1
time/date stamp
Posted: Thu Feb 13, 2003 8:55 am
by mzfp2
How can i use a time/date stamp to work out the difference in years between two stamps. The normal getdate functions counts from 1970, but i need to include dates before 1970
thanks
Muzz
Posted: Thu Feb 13, 2003 9:01 am
by twigletmac
Where do the dates come from? Database? User Input? Elsewhere?
Mac
Posted: Thu Feb 13, 2003 9:04 am
by mzfp2
initially the date comes from a user input as 3 parts, month, date and year. This is then converted to a time using the mktime function.
Posted: Thu Feb 13, 2003 9:05 am
by twigletmac
Since the data is coming from user input and will contain dates earlier than 1970 then it would probably be best not to convert the dates to timestamps. Is determining a difference in years the only calculation you need to do using the dates, or are there others?
Mac
Posted: Thu Feb 13, 2003 9:11 am
by mzfp2
yes there are other calculations to consider too, the main ones are, how days ago the user last logged in (not necessarily in the same month or year), the age of the person depending upon the date of birth and the current date.
For these a timestamp seems the easiest option because it allows calculations to be treated as any other integer calculation.
So .. if i need dates before 1970 would i have to dump the timestamp and store day month and year seperate?
Posted: Thu Feb 13, 2003 9:30 am
by twigletmac
For calculating last login times a timestamp would be most appropriate because it's pretty much impossible for them to have last logged in in 1969.
For calculating the age of someone you could do something like this which uses the year inputted to get a rough age and then checks using timestamps that the day has passed and if not subtracts one from the rough age:
Code: Select all
<?php
$age = date('Y') - $_POST['yearborn'];
$agets = mktime(0, 0, 0, $_POST['monthborn'], $_POST['dayborn']);
$datedif = mktime() - $agets;
if ($datedif < 0) {
--$age;
}
echo $age;
?>
Having never had to work with older dates before I've just kinda come up with this off the top of my head, someone else may know of a function that can do this that I've missed.
Mac
Posted: Thu Feb 13, 2003 9:30 am
by aybra
You could also try exploring doing all the calculations in your database, MySql will do that form of date calculation rather easily.