在 PHP 中将 TIMESTAMP 转换为 unix 时间?
目前我将时间存储在数据库中,如下所示: 2010-05-17 19:13:37
但是,我需要比较两次,我觉得如果它是一个会更容易unix 时间戳,例如 1274119041
。 (这两个时间是不同的)
那么如何将时间戳转换为unix时间戳呢?有没有一个简单的php函数可以实现?
Currently I store the time in my database like so: 2010-05-17 19:13:37
However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041
. (These two times are different)
So how could I convert the timestamp to unix timestamp? Is there a simple php function for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在寻找 strtotime()
You're looking for strtotime()
您想要 strtotime:
You want strtotime:
获取 unixtimestamp:
转换为 mysql 日期时间格式:
获取一些 mysql 时间戳:
将其转换为 unixtimestamp:
...将其与一个或一系列时间进行比较,以查看用户是否输入了实际时间:
Unix 时间戳也更安全。在检查(例如)之前的范围检查之前,您可以执行以下操作来检查 url 传递的变量是否有效:
Getting a unixtimestamp:
Converting to mysql datetime format:
Getting some mysql timestamp:
Converting it to a unixtimestamp:
...comparing it with one or a range of times, to see if the user entered a realistic time:
Unix timestamps are safer too. You can do the following to check if a url passed variable is valid, before checking (for example) the previous range check:
如果您使用 MySQL 作为数据库,它可以使用 UNIX_TIMESTAMP:
您也可以在 PHP 端使用 strtotime:
If you're using MySQL as your database, it can return date fields as unix timestamps with UNIX_TIMESTAMP:
You can also do it on the PHP side with strtotime:
如果你将时间存储在数据库中,为什么不让数据库也给你它的unix时间戳呢?请参阅
UNIX_TIMESTAMP(日期)
,例如。数据库还可以进行日期和时间比较和算术。
if you store the time in the database, why don't you let the database also give you the unix timestamp of it? see
UNIX_TIMESTAMP(date)
, eg.databases can also do date and time comparisons and arithmetic.