Current location: Hot Scripts Forums » Programming Languages » PHP » inserting and retrieving date and time from mysql database


inserting and retrieving date and time from mysql database

Reply
  #1 (permalink)  
Old 07-26-04, 02:07 AM
stealth04 stealth04 is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
inserting and retrieving date and time from mysql database

Hi,
I am a beginner, so I have some problems with coding.
I would like to create a script which would insert date and time informations into the database and then read those informations.
I have created a database with fields date (type date) and time (type time).
I have written:
.....insert into information(date, time) values('$date', '$time')....
could anyone help me defining $date and $time variables?

How to change format when I will read these iformations from the database?
Thank you.
Regards,
Niko
Reply With Quote
  #2 (permalink)  
Old 07-26-04, 10:04 AM
kvnband kvnband is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 242
Thanks: 0
Thanked 0 Times in 0 Posts
Just wondering....Have you even looked at the manual?

http://www.php.net/manual/en/ref.datetime.php
Reply With Quote
  #3 (permalink)  
Old 07-26-04, 04:58 PM
Spirit Spirit is offline
Newbie Coder
 
Join Date: Jun 2004
Location: University of California, Irvine
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hi, Niko! Yeah, I had trouble w/dates and times at first, too. I got confused the most with the word "timestamp". In the MySQL manual, there are several variations of the timestamp type and it took me a while to realize my use of it was in yyyymmddhhiiss format. In the PHP manual, it referred to timestamps but it was referring to an integer (seconds since unix epoch).

You can store dates and times in the db however you want. What I do is parse the dates/times to comoponent parts and then convert the dates to unix epoch integers using PHP's mktime() function. Then, you can show your dates and times in any format you want using PHP's date() function, e.g. date ("m-d-Y g:i:s a", $myunixepochfield)

This is what I do...

formpage > transitionpage (INSERT...) auto-refreshes to > confirmpage (SELECT...)

Hope this helps!
--ST
Reply With Quote
  #4 (permalink)  
Old 07-27-04, 05:04 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
if you want to insert current time and date this should do:
PHP Code:

$query mysql_query("insert into information set date=CURDATE(), time=CURTIME()"); 

Reply With Quote
  #5 (permalink)  
Old 07-27-04, 07:56 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
What are you trying to store this for? Maybe a simple timestamp field might be a better solution.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Reply With Quote
  #6 (permalink)  
Old 07-29-04, 11:19 AM
stealth04 stealth04 is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
I have done this way:
$date = date("Y-m-d");
$time= date("H:i:");
"insert into guestbook (comment, name, email, site, date, time, userip) values(...'$date', '$time'...);
This works for now.
I would like to do a guestbook and later I would like to add some functions to sort comments...
Is there any better way to store date and time information?
I don't have much experience with PHP and mySQL and I am still learning...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 04:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.