homepage Welcome to WebmasterWorld Guest from 54.161.202.234
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP date help
dowzer




msg:4119808
 3:44 am on Apr 22, 2010 (gmt 0)

I have created an HTML form and I have it so the data from the form is entered into a database and stored - the one thing I cannot seem to do is get it to automatically insert the date and time of the form submission into the database.

I have a field called postdate in my database with a type of date - what do I need to do to get the post submission date and time put into that field? I would prefer it as an actual date and time which I can read rather than using seconds passed since 1970 or whatever it is.

Thanks

Jason (living proof that a little knowledge is dangerous!)

 

rocknbil




msg:4119826
 4:27 am on Apr 22, 2010 (gmt 0)

rather than using seconds passed since 1970 or whatever it is.


This "kind of" depends on how your postdate field is created.

Alter table your_table change postdate postdate datetime not null default '0000-00-00 00:00:00';

There. Fixed it for you. :-) If you don't like that format, see below.

what do I need to do to get the post submission date and time put into that field?


Add this into the other values you submit to the DB:

insert into your_table (postdate) values(now());

Done. This will put your mysql server's time as of now into the field. There are cases where the server time is different than the mysql server time, and if it is . . . complain, it needs to be fixed (unless they are geographically different, 'nother story.)

If your server is in a different time zone than you expect, you may have to adjust, topic for another thread.

For the precise formatting you want, have a look at the date_format() section of the date and time functions [dev.mysql.com] manual page.

You'd just select the date format you want when selecting them:

select date_format(postdate, '%b %d %Y %r') from your_table;

--> Apr 21 2010 09:27:45 PM

The cool thing is, you can have a unix timestamp field as a date field, and those functions will work as well as they do on a formatted datetime field.

dowzer




msg:4120967
 6:52 pm on Apr 23, 2010 (gmt 0)

Awesome, thank you - MOST grateful :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved