Reputation: 2229
I have a really basic question concerning unix timestamp and mysql date. I'm trying to build a small website where users can register and fill in their birthdate.
Problem is that unix starts with Jan 01 1970. Now if i calculate age for users, form dates like date('m.d.Y', $unix_from_db) and so on it will fail with users older that 40 years, right?
So what would be the rigth way for doing this. Sorry, for basic question like this, but I'm inexperienced with php and mysql.
//Edit: Thank you all. I got confused, because, date('m.d.y',mktime(0,0,0,1,1,1890)) and date('m.d.y',-2000) returned 01.01.70 - next time I'll study manual more carefully. I was able to fix my site, thanks again. Too bad I can accept only one answer, they were all equally good.
Upvotes: 4
Views: 2754
Reputation: 27482
So what? Arithmetic works with negative numbers. When they start the epoch makes no difference as long as it's used consistently.
Negative numbers are people too! Don't discriminate! It hurts their feelings.
Upvotes: 4
Reputation: 16553
On dates before the epoch the number still increases, thus becoming less negative, as time moves forward.
http://en.wikipedia.org/wiki/Unix_time
You shouldn't have any problem with pre epoch dates:
<?php
echo date("m-d-Y", mktime(0, 0, 0, 1, 1, 1970));
echo date("m-d-Y", mktime(0, 0, 0, 1, 1, 1969));
echo date("m-d-Y", mktime(0, 0, 0, 1, 1, 1943));
?>
Outputs
01-01-1970
01-01-1969
01-01-1943
Upvotes: 3
Reputation: 1531
Unix timestamps for times before 1970 are negative integers, counting the number of seconds until jan 1 1970.
For example, 2/13/1943
is -848343600
Upvotes: 5
Reputation: 62894
Store the date as a date or datetime in mysql.
Output it however you like with date:
date('<format>', strtotime($date_from_db));
strtotime will return some negative integer for dates before 1970. date will handle that just fine.
Upvotes: 2