SuperSpy
SuperSpy

Reputation: 1314

What is the MYSQL variant of time()?

In my script I store a PHP time() value in the database. I've got a simple solution, but I bet it is certainly not the most 'clean' solution.

   <?php
   $time = time(); 
   mysql_query("DELETE FROM table WHERE expire < '$time';");
   ?>

Is there an MySQL function with the same format as the PHP time() function?

Thanks in advance! PS: Please support my English by correcting me :)

EDIT: I'll go with the UNIX_TIMESTAMP(), thanks all, for helping me and for discussing about the best option.

Upvotes: 0

Views: 198

Answers (3)

Amir Raminfar
Amir Raminfar

Reputation: 34150

Yes there is. You can use the now function.

mysql_query("DELETE * FROM table WHERE expire < NOW();");

Edit: I encourage you to use this function and not UNIX_TIMESTAMP. Reason is that you should have your column type as DATE and not long. In that case, you can do time functions on a date column where you can't easily do on a time stamp value.

Upvotes: 3

delphist
delphist

Reputation: 4539

SELECT UNIX_TIMESTAMP(); // returns 1298999053 for example

so:

mysql_query("DELETE * FROM table WHERE expire < UNIX_TIMESTAMP()");

Upvotes: 2

Galz
Galz

Reputation: 6832

In MySQL the function is called now().

DELETE * FROM table WHERE expire < now();

Upvotes: 1

Related Questions