clarification: time() in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

clarification: time() in PHP

Post by raghavan20 »

I have a database that has got a Time field with datatype timestamp(14)
but if I try to do sth like:
insert into ChatUsers_tbl values('NULL', 'Raghavan', '".time()."');

time() is of 10 digits but doesnot get into the mysql database which is of 14 digits.

Instead when I insert I get 'fourteen zeros' in the field.

What is the best way to store a timestamp?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I always store it as a long interger.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Might want to read http://dev.mysql.com/doc/mysql/en/datetime.html

Code: Select all

// using MySQL current timestamp
$query = "INSERT INTO foo VALUES (NOW());";
// using PHP current timestamp
$query = "INSERT INTO foo VALUES (FROM_UNIXTIME(" .  time() . "))";
I believe http://adodb.sf.net has a class dedicated to time handling...
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I was trying something like this but it still thinks it as wrong format
I have got the field with timestamp(14) in Mysql.

Code: Select all

echo "<br />".date(YmdGis);
ex output: 20050809164830(14 digits)

1. What do you think is the problem now?
2. The only difference btw DateTime and Timestamp in mysql is the former one has got separators btw each part...is it true?
the later has got no separators btw any of the parts?

I am actually looking for time and date in a field and it could be sorted.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

just use NOW() in the SQL like timvw said..
Post Reply