Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 25th, 2007, 6:54 AM   #1
Satans_Banjo
Newbie
 
Join Date: Oct 2006
Posts: 15
Rep Power: 0 Satans_Banjo is on a distinguished road
Entering DATETIME values with PHP

Hi

I'm pretty new to PHP/MySQL and I am having trouble entering dates into a database. I've got the database set up and one of the fields is a DATETIME field. What I want to do is put the time that the record was entered into that file. This is what I'm currently doing, but it leaves the date as the default value in the database (0000-00-00 00:00:00):

[PHP]$phpTime = time();
$mySqlTime = gmdate("YYYY-mm-dd H:m:s", $phpTime);
$dateAdded = strtotime($mySqlTime . " GMT");[/PHP]

Is that completely wrong or am I just missing something trivial and/or blindingly obvious?

Thanks,

Banjo
Satans_Banjo is offline   Reply With Quote
Old Jun 25th, 2007, 7:43 AM   #2
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
With this, you get time(), format it in the second line, then reconvert it to time with strtotime, which wont be stored correctly in the database. In fact, your conversion in the second line is completely off, since gmdate is basically the same as date and will result in "2007200720072007-" etc.

Really, you should just set the field to INT(11) and store time(). It's much simpler to work with.

But if you really want to keep that format, you would do gmdate('Y-m-d H:m:s', $phpTime) and add that with your query, not $dateAdded, unless you wanted a GMT version of time().
Styx is offline   Reply With Quote
Old Jun 25th, 2007, 7:50 AM   #3
Satans_Banjo
Newbie
 
Join Date: Oct 2006
Posts: 15
Rep Power: 0 Satans_Banjo is on a distinguished road
Thanks for the quick reply. I think I'll keep the format so that I can do queries like showing items posted after a certain date etc.

It's working now. Cheers!

EDIT: One more thing: how do I make it so that the time is in GMT, or make an offset to the server time or something like that? The server is somewhere in Germany so the time isn't in GMT
Satans_Banjo is offline   Reply With Quote
Old Jun 25th, 2007, 9:32 AM   #4
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
gmdate should return a GMT version, but check the documentation for the function, or see the other functions listed on the side for something that might help.
Styx is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php with Sun Java System Application Server Platform lucifer PHP 0 Jun 7th, 2007 5:03 AM
Classes in PHP kruptof PHP 9 May 11th, 2007 10:44 PM
C# Datetime data types King C# 2 Jun 5th, 2006 12:21 AM
Returning two values Navid C 19 Jun 13th, 2005 3:57 AM
Problem Inserting Values into mySQL from PHP stakeknife PHP 1 Mar 23rd, 2005 9:45 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC