PHP strtotime 和 MySQL 日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7732246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP strtotime and MySQL date format
提问by Joseph Duffy
I'm trying to get my function to add 1 day to the date I send it ,but I can;t figure out how to match the MySQL formatting. I've currently got:
我试图让我的函数在我发送的日期上增加 1 天,但我无法弄清楚如何匹配 MySQL 格式。我目前有:
$result = mysql_query($query);
$lastdate = mysql_result($result, 0, 'date');
$date = strtotime(date("Y-m-d", strtotime($lastdate)) . " +1 day");
I know the $date =
line is incorrect somewhere, but I don't understand the function too well. It's being given the date in the format YYYY-mm-dd ($query is just getting the last date in the database), due to how MySQL stores dates.
I'm guessing that using the strtotime function isn't the right thing to do, or I've got the format/idea all wrong.
Thanks for any help, this is annoying me now :(
我知道该$date =
行在某处不正确,但我不太了解该功能。由于 MySQL 存储日期的方式,它以 YYYY-mm-dd 格式给出日期($query 只是获取数据库中的最后一个日期)。
我猜想使用 strtotime 函数不是正确的做法,或者我的格式/想法都错了。
感谢您的帮助,这现在让我很烦:(
回答by Levi Morrison
I think you want $date = date("Y-m-d", strtotime("+1 day", $lastdate))
.
我想你想要$date = date("Y-m-d", strtotime("+1 day", $lastdate))
。
You may need to convert $lastdate
using strtotime
.
您可能需要$lastdate
使用strtotime
.
回答by Daren Chandisingh
You could just add 86400 seconds to the result from strtotime(), since that returns an integer number of seconds since 1/1/1970, and there are 86400 seconds in a day.
您可以将 86400 秒添加到 strtotime() 的结果中,因为它返回自 1970 年 1 月 1 日以来的整数秒数,并且一天中有 86400 秒。
$lastdate = '2011-10-11 22:07:11';
$date = date("Y-m-d", strtotime($lastdate) + 86400);
echo $date;
Outputs:
输出:
2011-10-12