PHP: why strtotime () returns "NOW ()"?

I call strtotime() on a formatted datetime string and for some reason always returns NOW() ...

If my formatted date and time string (stored in the last_seen attribute): 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 is going on here? This is definitely not the expected result. The string is stored in the MySQL database, if that matters.

Edit: upon request, the code used to create the attribute in the database:

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

Edit 2: on request, the code used to pull out the user table with the right user:

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

(not very useful, lol).

Edit 3: if I var_dump($user->last_seen) result:

 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:

 bool(false) 

Edit 5: This problem was the result of being an idiot, but some subtle debugs were done by everyone who posted. Read the answers if you are interested.

+4
source share
3 answers

First of all, you check the return value of strtotime($user->last_seen)

If strtotime($user->last_seen) returns false , then $user->last_seen may be empty or invalid. Date and time formats

 var_dump(strtotime($user->last_seen)) 
+4
source

Your problem, most likely, is not in the select clause, but lies where you store your data.

When you insert time into the database, you probably do

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

instead

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

So you need to lose the quotes there ...

You do not save time in the database, but the line now() ...

The best you could do is change the database column type from varchar to DateTime , so even the database knows this DateTime. Then you do not need to return it back to DateTime in PHP.

+1
source

Oh my god, I'm going to kick myself for it. I made the whole project Ctrl+F for last_seen , and I found that in my routing scheme I had this:

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

Walking with a tail between his legs ...

+1
source

Source: https://habr.com/ru/post/1485941/


All Articles