如何在PHP中保存早于时间戳的日期 [英] How to save dates older than timestamp in PHP

查看:101
本文介绍了如何在PHP中保存早于时间戳的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但我找不到答案.

This should be an easy one but I just can't find the answer.

我有一个输入字段,用户可以在其中输入生日日期(DD.MM.YYYY),并使用strtotime()将其插入MySQL的整数字段中,因为该日期时间更快(并且我有很多记录)

I have an input field in which the user enters his birthay date (DD.MM.YYYY) and with strtotime() I insert it into integer field in MySQL because it's faster that datetime (and I have a lot of records).

但是很明显会有早于1.1.1970的用户,因此时间戳将为零. 在网页上,我想显示用户的年龄.因此,如果他出生于1960年,那么他将是51岁. 我知道我可以将MySQL字段更改为DATETIME,但是PHP仍然可以用来计算用户的年龄,并且它使用UNIX时间戳.

But obviously there will be users older than 1.1.1970 so the timestamp would be zero. On the webpage I want to show how old is the user. So if he is born in 1960 he would be 51 years old. I know that I could change MySQL field to DATETIME but still the PHP would be the one to calculate the age of user and it uses UNIX timestamp.

那么,我应该如何组织它? 谢谢!

So, how should I organize it? Thanks!

推荐答案

在处理时,应使用 DateTime 对象PHP中的日期和时间.解析日期格式时,它非常灵活,可以计算两个日期之间的差,并且没有1970或2038问题.

You should use the DateTime object when handling dates and times in PHP. It is very flexible when parsing date formats, it can calculate the difference between two dates and it has no 1970 or 2038 problem.

这篇关于如何在PHP中保存早于时间戳的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆