![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Posts: 15
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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(). |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2006
Posts: 15
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Php with Sun Java System Application Server Platform | lucifer | PHP | 0 | Jun 7th, 2007 4:03 AM |
| Classes in PHP | kruptof | PHP | 9 | May 11th, 2007 9:44 PM |
| C# Datetime data types | King | C# | 2 | Jun 4th, 2006 11:21 PM |
| Returning two values | Navid | C | 19 | Jun 13th, 2005 2:57 AM |
| Problem Inserting Values into mySQL from PHP | stakeknife | PHP | 1 | Mar 23rd, 2005 8:45 AM |