| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |
|

09-05-07, 15:07
|
|
Registered User
|
|
Join Date: Jun 2007
Location: London
Posts: 117
|
|
|
DateTime in mysql
|
|
Hello all,
I insert into my database using php a variable date which i get from php's date() function. This is inserted into the database. When i echo the contents i get this:
2030-08-07 07:31:00 (example)
What is the extra 20 for, every row contains this 20 infront of the date? Is this something mysql does on purpose?!
Thanks all, i appreciate any help
|
|

09-05-07, 16:06
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
it means you have entered the dates incorrectly

|
|

09-05-07, 16:24
|
|
Registered User
|
|
Join Date: Jun 2007
Location: London
Posts: 117
|
|
|
|
Quote:
|
Originally Posted by r937
it means you have entered the dates incorrectly

|
 I don't think thats the case  - since this is what i did:
PHP Code:
<?php
$subject = $_POST['title'];
$text = $_POST['msgpost'];
$date = date('d-m-y:h-i');
$query = sprintf("INSERT into threa (subject, text, date) values ('$subject', '$text', '$date')") ;
mysql_query($query) or die(mysql_error());
?>
Is there anything wrong with this that could cause a entry into the database like this: 2030-08-07 07:31:00 for the date field.
|
|

09-05-07, 16:37
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
well, i don't do php, but i can read it, and maybe even modify it
Code:
<?php
$subject = $_POST['title'];
$text = $_POST['msgpost'];
$query = sprintf("INSERT into threa (subject, text, date) values ('$subject', '$text', current_timestamp ) ") ;
mysql_query($query) or die(mysql_error());
?>
feel the force, luke
dates must be specified in year-month-day sequence -- you had day-month-year sequence, and you just happened to use values which could be interpreted as year-month-day, except that the year was assumed to be in this centry, which is why the 20 got prepended
it's much easier (and more efficient) just to tell the database to use the current timestamp, rather than trying to construct that string yourself -- for one thing, the database first has to parse your date string and try to figure it out, which takes extra processing
passing in a date string is not wrong if you have to do it, but when it's really just the current timestamp you want, that's what that special SQL keyword was invented for
neat, eh? 
|
Last edited by r937; 09-05-07 at 16:42.
|

09-05-07, 17:01
|
|
Registered User
|
|
Join Date: Jun 2007
Location: London
Posts: 117
|
|
Quote:
|
Originally Posted by r937
feel the force, luke
neat, eh? 
|
Hell yes, thank you luke
It is much more efficient, less likely for an error to occur since were dealing directly with the sql.
I really need to find a list of sql statements before i start writing duplicate scripts!!
EDIT: for the date field, it is a DATETIME type, this is ok right, it will give me back the date and time(hours:minutes hopefully)
|
|

09-05-07, 17:12
|
|
Registered User
|
|
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
|
|
|
|

09-05-07, 17:15
|
|
Registered User
|
|
Join Date: Jun 2007
Location: London
Posts: 117
|
|
Quote:
|
Originally Posted by dbmab
|
Good point - its just that there are so many words. 
|
|

09-06-07, 06:13
|
|
Registered User
|
|
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
|
|
I suspect the PHP code you were looking for is :
Code:
$date = date('Y-m-d h:i:s');
Might also be worth noting that you can input dates into your DB datetime column just using YYYY-MM-DD , and the hours mins secs will be zeros (00) .
So if you're just doing this use NOW() instead of CURRENT_TIMESTAMP. Also if you want to put in the date and time now + 1 hour, then you can do
DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 1 HOUR)
Hope these help.
|
|

09-06-07, 06:18
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
Quote:
|
Originally Posted by aschk
So if you're just doing this use NOW() instead of CURRENT_TIMESTAMP.
|
no, don't
first of all, they are the same thing, and secondly, CURRENT_TIMESTAMP is the standard SQL keyword, NOW() is proprietary mysql syntax
and if you want the date only, with no time or with 00:00:00 as the time, use CURRENT_DATE
|
|

09-06-07, 06:50
|
|
Registered User
|
|
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
|
|
Aaah thats probably what I was looking for. I thought NOW() was date only? Checking MySQL manual now :P
|
|

09-06-07, 12:01
|
|
Registered User
|
|
Join Date: Jun 2007
Location: London
Posts: 117
|
|
Quote:
|
Originally Posted by r937
it's much easier (and more efficient) just to tell the database to use the current timestamp, rather than trying to construct that string yourself -- for one thing, the database first has to parse your date string and try to figure it out, which takes extra processing
|
I've just realised. When i need the date and time from the database i need it in the form: dd-mm-yyyy hh-mm but the database seems to put the year first which will mean i have to do some work with php to re-arrange it. So it might be better just to create the string and input it into the db. Either way, php will have to be involved.
|
|

09-06-07, 12:12
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
the mysql DATE_FORMAT function was invented for just such a requirement
so neither way is php involved
|
|

09-06-07, 12:28
|
|
Registered User
|
|
Join Date: Jun 2007
Location: London
Posts: 117
|
|
Quote:
|
Originally Posted by r937
the mysql DATE_FORMAT function was invented for just such a requirement
so neither way is php involved
|
I really need to read  thanks again r937
|
|

09-06-07, 13:03
|
|
Registered User
|
|
Join Date: Jun 2007
Location: London
Posts: 117
|
|
ehhh...am i using the CURRENT_TIMESTAMP correctly:
PHP Code:
$query = sprintf("INSERT into $table (subject, content, date) values ('$subject', '$text', 'CURRENT_TIMESTAMP')") ;
mysql_query($query) or die(mysql_error());
Since i just got an error that i have never seen before?!
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(subject, content, date) values ('test', 'test ' at line 1
|
|

09-06-07, 13:33
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
smells like a php error
try your query outside of php to be sure
p.s. do not put CURRENT_TIMESTAMP in quotes
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|