将天数转换为 unix 时间戳
如何将天数转换为 unixtimestamp?
例如,如果用户在表单中输入 55(= 55 天),我希望将 55 天添加到当前时间,然后将其存储在 unix 时间戳中。
Possible Duplicate:
How do I add 24 hours to a unix timestamp in php?
How can I convert days to a unixtimestamp?
Example if a user inputs 55 (=55 days) in a form, I wish to add 55 days, to the current time, and then store it in a unix timestamp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
time()
进行算术:或者可能使用
strtotime()
:Arithmetic with
time()
:or perhaps using
strtotime()
:显而易见的解决方案是,UNIX 时间戳以秒为单位,因此只需将天数乘以一天中的秒数 (86400),然后将其添加到当前时间 (
time()
)。但是,如果您正在做的事情甚至比这稍微复杂一点,请让 PHP 来计算“一天”的含义并使用 日期时间类。例如:
这些类比手动计算更灵活,而且当您了解它们时,也更直观。
The obvious solution is to say that a UNIX timestamp is in seconds, so just multiply the number of days by the number of seconds in a day (86400) and add that to the current time (
time()
).However, if you are doing anything even slightly more complex than this, leave it to PHP to work out what "a day" means and use the DateTime classes. For instance:
These classes are much more flexible – and, when you get to know them, much more intuitive – than doing the calculations manually.
使用
echo strtotime("+55 天");
http://php.net/manual/en/function.strtotime.php
use
echo strtotime("+55 days");
http://php.net/manual/en/function.strtotime.php
time()
已经返回一个 unix 时间戳,这是自 1970 年以来的时间(以秒为单位)。因此,要加起来 55 天,您应该添加 55 * SecondsPerDay 秒。time()
already returns a unix timestamp, which is the time in seconds since about 1970. So to add up 55 days, you should add 55 * secondsPerDay seconds.