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.

 
Go Back  dBforums > Database Server Software > MySQL > DateTime in mysql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-07, 15:07
compsci compsci is offline
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
Reply With Quote
  #2 (permalink)  
Old 09-05-07, 16:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
it means you have entered the dates incorrectly

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-05-07, 16:24
compsci compsci is offline
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.
Reply With Quote
  #4 (permalink)  
Old 09-05-07, 16:37
r937 r937 is offline
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?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 09-05-07 at 16:42.
Reply With Quote
  #5 (permalink)  
Old 09-05-07, 17:01
compsci compsci is offline
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)
Reply With Quote
  #6 (permalink)  
Old 09-05-07, 17:12
dbmab dbmab is offline
Registered User
 
Join Date: Apr 2006
Location: Denver, Co. USA
Posts: 240
Probably reading the manual section for what you are trying to use would help answer a lot of your questions - http://dev.mysql.com/doc/refman/5.0/en/datetime.html
Reply With Quote
  #7 (permalink)  
Old 09-05-07, 17:15
compsci compsci is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 117
Quote:
Originally Posted by dbmab
Probably reading the manual section for what you are trying to use would help answer a lot of your questions - http://dev.mysql.com/doc/refman/5.0/en/datetime.html
Good point - its just that there are so many words.
Reply With Quote
  #8 (permalink)  
Old 09-06-07, 06:13
aschk aschk is offline
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.
Reply With Quote
  #9 (permalink)  
Old 09-06-07, 06:18
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 09-06-07, 06:50
aschk aschk is offline
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
Reply With Quote
  #11 (permalink)  
Old 09-06-07, 12:01
compsci compsci is offline
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.
Reply With Quote
  #12 (permalink)  
Old 09-06-07, 12:12
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 09-06-07, 12:28
compsci compsci is offline
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
Reply With Quote
  #14 (permalink)  
Old 09-06-07, 13:03
compsci compsci is offline
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
Reply With Quote
  #15 (permalink)  
Old 09-06-07, 13:33
r937 r937 is offline
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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