mysql 和 php 中的时间戳
我正在尝试创建一个用于订阅和到期日期的数据库,我在数据库中添加了订阅日期作为时间戳字段,到期日期应该在一个月之后。我正在尝试添加一个月,但我发现的功能仅使用日期格式函数,我认为它们应该相同(时间戳和 date() ),但它给了我一个错误!
$startDate = time();
$conv = date('Ymd H:i:s',$startDate);
echo $conv;
echo ('
');
$dd=date('Ymd H:i:s', strtotime('+1 个月', $startDate));< /code>
echo $dd;
这个函数工作得很好,但是如果我用我保存在数据库中的值“TIMESTAMP”替换开始日期,就会出现这个错误” 遇到格式不正确的数值
有什么建议吗? 提前致谢
am trying create a database for a use with a subscription and expiration date,I added the subscription date in my database as a Timestamp field, and the expiration date should be after a month..am trying to add a month but the functions i found are only working with date format functions,i thought they should be the same(Timestamp and date() ) but its giving me an error!
$startDate = time();
$conv = date('Y-m-d H:i:s',$startDate);
echo $conv;
echo ('<br/>');
$dd=date('Y-m-d H:i:s', strtotime('+1 month', $startDate));
echo $dd;
this function works just fine but if I replace the start date with the value 'TIMESTAMP' i saved in the database it gives me this error"A non well formed numeric value encountered
any suggestions?!
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MySQL TIMESTAMP 不像 PHP 那样显示为整数。如果您查看文档,您会发现 TIMESTAMP 使用 19 个字符显示。
如果您想在数据库中存储秒数 - 使用 INT(无符号)类型而不是 TIMESTAMP。
换句话说,在使用代码之前运行 strtotime($mysql_timestamp) (将变量名称替换为适当的变量名称),以便将其从字符串转换为整数。
然而,您应该真正考虑一下在日期存储方面您是否做出了正确的选择 - 有一些非常有趣的函数可以处理 PHP 和 MySQL 中内置的日期。如果您尝试手动处理这一切(通过 1970 年以来的秒数),您将遇到许多问题(时区偏移、DST、无法存储 1970 年之前的日期等等)。
MySQL TIMESTAMP isn't displayed as integer like it is in PHP. If you check the documentation, you'll see that TIMESTAMP is displayed using 19 characters.
If you want to store number of seconds in your database - use INT (unsigned) type rather than TIMESTAMP.
In other words, run strtotime($mysql_timestamp) (replace variable name with appropriate one) before you use your code so it gets converted to an integer from string.
However, you should really consider whether you made the right choices when it comes to date storage - there are some very interesting functions to work with dates built in both in PHP and MySQL. If you try to handle it all manually (via number of seconds from 1970) you'll run into many problems (timezone offsets, DST, not being able to store dates BEFORE 1970 and so on).