简体   繁体   English

Unix时间戳和mysql日期:生日

[英]Unix timestamp and mysql date: birthdate

I have a really basic question concerning unix timestamp and mysql date. 我有一个关于unix时间戳和mysql日期的基本问题。 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('mdY', $unix_from_db) and so on it will fail with users older that 40 years, right? 问题是unix从1970年1月1日开始。现在,如果我计算用户的年龄,表单日期如日期('mdY',$ unix_from_db)等等,它将失败,用户年龄超过40年,对吧?

So what would be the rigth way for doing this. 那么这样做的方法是什么呢? Sorry, for basic question like this, but I'm inexperienced with php and mysql. 对不起,对于像这样的基本问题,但我对php和mysql缺乏经验。

//Edit: Thank you all. //编辑:谢谢大家。 I got confused, because, date('mdy',mktime(0,0,0,1,1,1890)) and date('mdy',-2000) returned 01.01.70 - next time I'll study manual more carefully. 我很困惑,因为,日期('mdy',mktime(0,0,0,1,1,1890))和日期('mdy', - 2000)返回01.01.70 - 下次我将学习更多手册小心。 I was able to fix my site, thanks again. 我能够修复我的网站,再次感谢。 Too bad I can accept only one answer, they were all equally good. 太糟糕了我只接受一个答案,他们都同样好。

Unix timestamps for times before 1970 are negative integers, counting the number of seconds until jan 1 1970. 1970年以前的Unix时间戳是负整数,计算直到1970年1月1日的秒数。

For example, 2/13/1943 is -848343600 例如,2/ 2/13/1943-848343600

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. 这伤害了他们的感情。

On dates before the epoch the number still increases, thus becoming less negative, as time moves forward. 在时代之前的日期,数字仍在增加,因此随着时间的推移变得越来越不利。

http://en.wikipedia.org/wiki/Unix_time 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

Store the date as a date or datetime in mysql. 将日期存储为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. strtotime将返回1970年之前的日期的一些负整数。日期将处理这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM