0

I have a SQL-query sent from PHP to MySQL, which f.e. looks like this:

SELECT (now()-table.created) FROM table 

Now I need to replace the MySQL-function "now()" with a a date from PHP (because it's not always "now" I want in the query), but it doesn't work when I pass PHPs "time()" to MySQL.

What PHP method could I use?

2
  • I think I just found it: $date = date('Y-m-d H:i:s'); - correct? Commented Apr 17, 2013 at 18:37
  • 1
    Use DateTime Commented Apr 17, 2013 at 18:37

3 Answers 3

2

MySQL has a function called UNIX_TIMESTAMP and FROM_UNIXTIME,

-- NOW() => time() SELECT UNIX_TIMESTAMP(NOW()) -- time() => DATETIME() SELECT FROM_UNIXTIME(1366223878) 
Sign up to request clarification or add additional context in comments.

8 Comments

I hate from deep of my heart append PHP into a MySQL query.
ok, I understand your approach and although I had to think more about your solution it seems to be the best... thanks!
Won't this still inflict time-inconsistancies? I'm stumped. If the time gets fetched at that same point (as said in the OP) it's still inaccurate.
can't I use the time()-function in PHP and pass it inside "FROM_UNIXTIME" - I know @WesleySchleumer will "hate it from deep of his heart" but wouldn't that be correct?
yep, seems to work, f.e. FROM_UNIXTIME(1366224926) gives me the actual time (a few minutes ago :)) and the value 1366224926 is taken from php, exactly what I need.
|
1

Hm, well, it depends on the format that table.created is stored in.

If it's a UNIXTIME value (i.e. the same format as PHP's time), then all you need to do is interpolate it in the right way:

<?php $dbobj->query('SELECT ('.time().'-table.created) FROM table'); ?> 

... if, instead, table.created is a datetime or similar value, you'll need to create the appropriate date format:

<?php $dbobj->query("SELECT ('".date('Y-m-d H:i:s', time())."'-table.created) FROM table"); ?> 

5 Comments

thanks! Actually it's "timestamp", so I'll use "date('Y-m-d H:i:s');", correct?
Yep, for your purposes here "timestamp" and "datetime" would be handled in the same way.
It's strongly recommended to use the newer class-approaches in PHP.
date() doesn't appear to be deprecated or recommended against on the documentation page. You may get a lot more from the newer class approaches, but for a simple case like this where you're taking an explicit epoch time and converting it into an explicit format, date() should be perfectly acceptable. That said, learning newer, more robust methods never hurt anyone, so I can't recommend against your suggestion.
You are correct. The new class also re-uses the old approaches. It's just nice, clean and a lot easier to manipulate the time with DateTime, as you will always access the methods inside the object and directly manipulate that time. Also it's a big plus you can easily add/subtract and see differences between different date-objects. Last but not least, timezone and offset operations are way easier with DateTime. Enough reasons to let the old functions go :)
0

Use the newer DateTine object;

$date = new DateTime('NOW'); $query = "SELECT * FROM table where date > ".$date->format('Y-m-d H:i:s') 

The NOW in the DateTime object will be accurate to your liking, as it's stored on the moment you seem to want it, unlike when done in MySQL, which may be (micro/milli)seconds later.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.