Current Date and Time

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Current Date and Time

Post by AliasBDI »

In phpMyAdmin, I have a table that contiains a 'datetime' field. When adding a new record to that table, the 'datetime' field automatically sets according to the currect date and time.

I created an online form to create a new record but found that it does not add the current date and time like phpMyAdmin. I suppose it is a PHP script. Does anyone know what it is? or how this works?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Nobody???

Post by AliasBDI »

Surely somebody knows how that works.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

mysql.com

Post by phpScott »

I'm pretty darn sure that most of the users here know how to use date and time functions or at least how to look them up.

If it is mysql db related try mysql.com

But since you didn't seem to know about this reference try this link

http://www.mysql.com/doc/en/Date_and_ti ... tions.html

and about a third of the way down they talk about now() and UNIX_TIMESTAMP() and the like

phpScott
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I don't know MySQL (I use PostGreSQL), but in PostGreSQL I would do what you want by doing something like

Code: Select all

CREATE TABLE foo (
   fooid SERIAL PRIMARY KEY,
   date TIMESTAMP NOT NULL DEFAULT now(),
   record TEXT NOT NULL
);
Now I've never stried using now() as a default in pgsql, but I think it works and doesn't get resolved at table creation time, but at row insert time. (SERIAL is like MySQL's AUTO INCREMENT.)

I would then normally insert into foo using

Code: Select all

INSERT INTO foo (record) VALUES('Stuff to save');
Values for both fooid and date will be automatically generated and inserted to make a complete row.
Post Reply