简体   繁体   English

PHP:为什么strtotime()返回“ NOW()”?

[英]PHP: why is strtotime() returning “NOW()”?

I'm calling strtotime() on a formatted datetime string and for some reason it always returns NOW() ... 我在格式化的日期时间字符串上调用strtotime() ,由于某种原因,它总是返回NOW()

If my formatted datetime string (stored in the last_seen attribute) is: 2013-06-13 07:13:04 如果我的格式化日期时间字符串(存储在last_seen属性中)是: 2013-06-13 07:13:04

and I write the following code: 我编写以下代码:

echo $user->last_seen;
echo date('F j, Y', strtotime($user->last_seen));

The output I get is: 我得到的输出是:

NOW() January 1, 1970

What on earth is going wrong here??? 这里到底出了什么问题??? This is definitely not the expected result. 这绝对不是预期的结果。 The string is stored in a MySQL database, if that makes any difference. 该字符串存储在MySQL数据库中(如果有区别的话)。

Edit: by request, the code used to create the attribute in the database is: 编辑:根据请求,用于在数据库中创建属性的代码为:

$user->last_seen = date('Y-m-d H:i:s');
$user->save;

Edit 2: by request, the code used to pull the users table, with the user we want, is: 编辑2:根据请求,用于与我们想要的用户一起提取用户表的代码为:

$user = User::find($user_id);

(not very helpful, lol). (不是很有帮助,大声笑)。

Edit 3: if I var_dump($user->last_seen) the result is: 编辑3:如果我var_dump($user->last_seen)结果是:

object(Laravel\Database\Expression)#40 (1) { ["value":protected]=> string(5) "NOW()" }

Edit 4: If I echo var_dump(strtotime($user->last_seen)) the result is: 编辑4:如果我回显var_dump(strtotime($user->last_seen)) ,结果是:

bool(false)

Edit 5: this problem was a result of me being an idiot, but some fine debugging was done by everyone who posted. 编辑5:这个问题是我是个白痴的结果,但是张贴的每个人都进行了一些精细的调试。 Read the answers if you are interested. 如果您有兴趣,请阅读答案。

First of all You check the return value of strtotime($user->last_seen) 首先,您检查strtotime($user->last_seen)的返回值

If strtotime($user->last_seen) returns false then $user->last_seen may be empty or not a valid Date and Time Formats 如果strtotime($user->last_seen)返回false$user->last_seen可能为空或无效的日期和时间格式

var_dump(strtotime($user->last_seen))

Your problem is most likely not in the select clause, but lays there where you store your data. 您的问题很可能不在select子句中,而是位于您存储数据的地方。

When you are inserting the time in the database, you are probably doing 当您在数据库中插入时间时,您可能正在做

insert into myTable (fieldname) values ('now()');

instead of 代替

insert into myTable (fieldname) values (now());

So you need to lose the quotes there... 所以你需要在那丢掉报价...

You are NOT storing the time in the database, but the string now() ... 您不是将时间存储在数据库中,而是字符串now() ...

The best thing you could actually do is change the database column type from varchar to DateTime , so even the database knows it's a DateTime. 您实际上可以做的最好的事情是将数据库列类型从varchar更改为DateTime ,因此即使数据库也知道这是DateTime。 Then you avoid having to cast it back to DateTime in PHP. 然后,您不必将其强制转换回PHP中的DateTime。

Oh dear god.... I'm going to kick myself for this one. 哦,亲爱的上帝。...我要为此踢自己。 I did a project wide Ctrl+F for last_seen and I discovered that in my routing scheme I had this: 我为last_seen做了一个项目范围的Ctrl+F ,我发现在我的路由方案中我有这个:

Route::filter('before', function() {
    if (Auth::check()) {
        $user = Auth::user();
        $user->last_seen = DB::raw('NOW()');
        $user->save();
    }
});

Walks away with tail between legs... 两条腿之间的尾巴走开...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM