time/date stamp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

time/date stamp

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Where do the dates come from? Database? User Input? Elsewhere?

Mac
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
aybra
Forum Commoner
Posts: 56
Joined: Sun Nov 24, 2002 12:52 am

Post by aybra »

You could also try exploring doing all the calculations in your database, MySql will do that form of date calculation rather easily.
Post Reply