unemployment Posted May 2, 2011 Share Posted May 2, 2011 I'm trying to make $time be a variable with the current time and I want it to have the same format as mysql. I can't figure out what I am doing wrong here Wrror: Warning: date_format() expects parameter 1 to be DateTime, integer given $time = date_format(time(),'Y-m-d H:i:s'); Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/ Share on other sites More sharing options...
Pikachu2000 Posted May 2, 2011 Share Posted May 2, 2011 What are you trying to do with it? Are you going to use it in a query string? Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209283 Share on other sites More sharing options...
unemployment Posted May 2, 2011 Author Share Posted May 2, 2011 What are you trying to do with it? Are you going to use it in a query string? Yes, I am going to use it in my where clause of a query string to append search results to an existing query results. Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209284 Share on other sites More sharing options...
Pikachu2000 Posted May 2, 2011 Share Posted May 2, 2011 You can use MySQL's NOW() function to return the current time into the query string. It's faster and more efficient, too. SELECT field1, field 2 FROM table WHERE your_time_field < NOW() Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209285 Share on other sites More sharing options...
unemployment Posted May 2, 2011 Author Share Posted May 2, 2011 You can use MySQL's NOW() function to return the current time into the query string. It's faster and more efficient, too. SELECT field1, field 2 FROM table WHERE your_time_field < NOW() Well the problem is that the time is a variable and will be changing. The first time the query runs it should pull results where time is less than the current time. The second time it will pull results where the time is less than the last row of the first query. That's why I need to make it a php variable. I also need it to be formatted so that I will be able to compare it to mysql Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209286 Share on other sites More sharing options...
Pikachu2000 Posted May 2, 2011 Share Posted May 2, 2011 For the first query you can use the NOW() function as above, For the second one, since the result is (presumably) already formatted from the DB, why can't you just use it directly? Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209287 Share on other sites More sharing options...
unemployment Posted May 2, 2011 Author Share Posted May 2, 2011 For the first query you can use the NOW() function as above, For the second one, since the result is (presumably) already formatted from the DB, why can't you just use it directly? Wouldn't that require me to duplicate the same function? It wouldn't be better to just pass date/time as a parameter? Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209288 Share on other sites More sharing options...
Pikachu2000 Posted May 2, 2011 Share Posted May 2, 2011 I don't think I can really answer with any degree of accuracy without seeing the code, but if you feel supplying the current time from php to the function as a parameter would be best, you can use this. $now = date( 'Y-m-d H:s:i', time() ); Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209289 Share on other sites More sharing options...
unemployment Posted May 2, 2011 Author Share Posted May 2, 2011 I don't think I can really answer with any degree of accuracy without seeing the code, but if you feel supplying the current time from php to the function as a parameter would be best, you can use this. $now = date( 'Y-m-d H:s:i', time() ); I'm not sure why but that doesn't seem to be working. I have... Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:23:09 ) UNION ALL ( SELECT ' at line 28 $now = date( 'Y-m-d H:s:i', time() ); $news_feed = fetch_personalized_news_feed($user_info['uid'], $now); WHERE users.id = ${uid} AND partners3.approved_date < ${time} Is that the correct formatting? Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209296 Share on other sites More sharing options...
Pikachu2000 Posted May 2, 2011 Share Posted May 2, 2011 It would need to be in quotes in the query string. You could possibly inject the quotes with the variable as part of the parameter, but you'll need to try it to see if it works. If not, just edit the query string to include them. $news_feed = fetch_personalized_news_feed($user_info['uid'], "'$now'"); Quote Link to comment https://forums.phpfreaks.com/topic/235323-date-time-format/#findComment-1209298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.