Jump to content

Archived

This topic is now archived and is closed to further replies.

unemployment

Date Time Format

Recommended Posts

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');

Share this post


Link to post
Share on other sites

What are you trying to do with it? Are you going to use it in a query string?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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()

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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() );

Share this post


Link to post
Share on other sites

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?

 

Share this post


Link to post
Share on other sites

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'");

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.