views:

73

answers:

4

Currently I store the time in my database like so: 2010-05-17 19:13:37

However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041. (These two times are different)

So how could I convert the timestamp to unix timestamp? Is there a simple php function for it?

+2  A: 

You're looking for strtotime()

Mark
Thanks this did the trick...but.. For somereason MySQL's NOW() is giving off a different time than TIME(). Any ideas about that?
Rob
meh, nevermind. I set a variable to time() and inserted that into the database rather than NOW(). Thanks anyway
Rob
+1  A: 

You want strtotime:

print strtotime('2010-05-17 19:13:37'); // => 1274123617
Matt
+1  A: 

If you're using MySQL as your database, it can return date fields as unix timestamps with UNIX_TIMESTAMP:

SELECT UNIX_TIMESTAMP(my_datetime_field)

You can also do it on the PHP side with strtotime:

strtotime('2010-05-17 19:13:37');
webbiedave
+1  A: 

if you store the time in the database, why don't you let the database also give you the unix timestamp of it? see UNIX_TIMESTAMP(date), eg.

SELECT UNIX_TIMESTAMP(date) ...;

databases can also do date and time comparisons and arithmetic.

ax