在php中将我的SQL时间戳更改为d-m-Y

在php中将我的SQL时间戳更改为d-m-Y,php,timestamp,strtotime,Php,Timestamp,Strtotime,在我的数据库中,我使用当前的时间戳来跟踪新用户的注册日期。 现在,我希望生成一个在过去7天或30天内注册的用户列表。 我想它会像本主题中解释的那样简单; 但是我当前的代码生成了一个意外的输出 for($i = 0; $i < $userlenght; $i++){ $comparedate = strtotime(date('Y-m-d', $user[$i]["date"]));

在我的数据库中,我使用当前的时间戳来跟踪新用户的注册日期。 现在,我希望生成一个在过去7天或30天内注册的用户列表。 我想它会像本主题中解释的那样简单;

但是我当前的代码生成了一个意外的输出

                for($i = 0; $i < $userlenght; $i++){
                    $comparedate = strtotime(date('Y-m-d', $user[$i]["date"]));
                    echo $comparedate . "<br/>";
                    if($user[$i]["date"] > $last){
                        //do stuff here with the users who match.
                    }
                }
($i=0;$i<$userlenght;$i++)的
{
$comparedate=strottime(日期('Y-m-d',$user[$i][“date”]);
echo$comparedate.“
”; 如果($user[$i][“date”]>last美元){ //在这里与匹配的用户进行交流。 } }
这将$comparedate值显示为“-3600”,我不知道为什么


有人能提供一些见解吗?

SQL时间戳是以字符串格式返回的,因此需要指示PHP以日期格式的值读取字符串。为此,您需要使用
strotime()

请尝试以下操作:

for($i = 0; $i < $userlenght; $i++){
       $comparedate = strtotime(date('Y-m-d', strtotime($user[$i]["date"])));
       echo $comparedate . "<br/>";
       if($user[$i]["date"] > $last){
             //do stuff here with the users who match.
       }
}
($i=0;$i<$userlenght;$i++)的
{
$comparedate=strottime(日期('Y-m-d',strottime($user[$i][“date”]));
echo$comparedate.“
”; 如果($user[$i][“date”]>last美元){ //在这里与匹配的用户进行交流。 } }
您的问题是,
$user[$i]['date']
是一个类似于
2018-09-14 11:09:10
的MySQL时间戳,并且不是
日期的有效输入。您只需删除对
date
的调用,您的代码就可以正常工作:

$comparedate = strtotime($user[$i]["date"]);

啊,看,在我能找到的其他主题中没有提到过类似的内容,但我想问题就在那里的某个地方。谢谢你,尼克!当我通过两个“strotime”函数对其进行过滤时,它只会生成一长串数字<代码>$comparedate=date('Y-m-d',strotime($user[$i][“date”])给了我一个格式很好的日期。然而,如果没有你的评论,我不会明白的,谢谢阿尔贝托!