【问题标题】:changing my SQL Timestamp into a d-m-Y in php将我的 SQL 时间戳更改为 php 中的 d-m-Y
【发布时间】:2018-09-14 13:31:16
【问题描述】:

在我的数据库中,我使用 CURRENT_TIMESTAMP 来跟踪新用户的注册日期。 现在我希望生成过去 7 或 30 天内注册的用户列表。 我认为它会像本主题中解释的那样简单; Get 30 days back date along with time

但我当前的代码产生了意外的输出。

                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.
                    }
                }

这会将 $comparedate 值显示为“-3600”,我不知道为什么。

谁能提供一些见解?

【问题讨论】:

    标签: php timestamp strtotime


    【解决方案1】:

    SQL Timestamp 以字符串格式返回,因此您需要指示 PHP 将字符串作为日期格式值读取。为此,您需要使用strtotime()

    尝试以下方法:

    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.
           }
    }
    

    【讨论】:

    • 当我通过两个“strtotime”函数过滤它时,它只会产生一长串数字。 $comparedate = date('Y-m-d', strtotime($user[$i]["date"])); 给了我格式正确的日期。但是,如果没有您的评论,我不会想到这一点,谢谢 Alberto!
    【解决方案2】:

    您的问题是$user[$i]['date'] 是一个类似于2018-09-14 11:09:10 的MySQL 时间戳,并且不是date 的有效输入。您可以删除对date 的调用,您的代码应该可以正常工作:

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

    【讨论】:

    • 啊,我能找到的其他主题中没有提到类似的内容,但我认为问题出在某个地方。谢谢你尼克!
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 2014-03-05
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多