UNIX_TIMESTAMP

Post here if you need help with SQL.
Post Reply
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

UNIX_TIMESTAMP

Post by Temor »

Hello, I'm wondering how UNIX_TIMESTAMP works in MySQL. I tried reading about it, but all that expert-mumbo-jumbo made absolutely no sense to me. Can anybody please explain a bit about it? :)

Also, I'm trying to make a " Users Online " function and I'm wondering what is the best way to insert and extract the date and time from a table?
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: UNIX_TIMESTAMP

Post by Tino »

One way to see it, essentially, is as the time() function in PHP. It just returns the amount of seconds since 1970-01-01 00:00:00
Please check out my CodeCanyon items.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: UNIX_TIMESTAMP

Post by jacek »

Temor wrote:Also, I'm trying to make a " Users Online " function and I'm wondering what is the best way to insert and extract the date and time from a table?
Inserting a timestamp would probably be easiest as you can do the maths with a single number much easier than if you stored a MySQL date format value.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: UNIX_TIMESTAMP

Post by Temor »

What is the most reliable way to insert a timestamp?
I really suck at MySQL, so I'm not really sure about anything when it comes to inserting time and dates in a table :P
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: UNIX_TIMESTAMP

Post by jacek »

You can use the UNIX_TIMESTAMP() function in the VALUES part of the INSERT query, for example,
INSERT INTO `table` (`list`, `of`, `columns`)
VALUES ('value', UNIX_TIMESTAMP(), 'other value');
would insert a timestamp into the of column in the table table.
Image
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: UNIX_TIMESTAMP

Post by Temor »

jacek wrote:You can use the UNIX_TIMESTAMP() function in the VALUES part of the INSERT query, for example,
INSERT INTO `table` (`list`, `of`, `columns`)
VALUES ('value', UNIX_TIMESTAMP(), 'other value');
would insert a timestamp into the of column in the table table.
Alright, I'll try it out :)
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: UNIX_TIMESTAMP

Post by Temor »

I've hit a snag... I got it to insert it properly. But it keeps repeating itself over and over every time I call the function. ON DUPLICATE KEY ENTRY gives me an error.

Here's my current SQL statement:
	$sql = " INSERT INTO `users_online` (`username`,`last_activity`,`null`)
			VALUES ('{$username}', UNIX_TIMESTAMP(),'null')";
			
	mysql_query($sql);
Please help me :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: UNIX_TIMESTAMP

Post by jacek »

Temor wrote:But it keeps repeating itself over and over every time I call the function
What do you mean by that.
Temor wrote:ON DUPLICATE KEY ENTRY gives me an error.
I think you mean ON DUPLICATE KEY UPDATE, also what is the error ?
Image
Post Reply