NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP & MYSQL DATE Function?

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 11-02-2009, 08:43 AM THREAD STARTER               #1 (permalink)
NamePros Regular
 
BuLLDoG's Avatar
Join Date: Aug 2003
Location: England
Posts: 934
BuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of light
 



Help! PHP & MYSQL DATE Function?


Hi,

I am trying to insert a date into a field in the format of YYYY-MM-DD. I have tried several ways, but it isn't inserting into the database correctly.

My SQL statement I am using is:

PHP Code:
$sql="INSERT INTO $tbl_name(itpname, detail, author, externalurl, datetime)VALUES('$itpname', '$detail', '$author', '$externalurl', curdate())"
????: NamePros.com http://www.namepros.com/programming/620615-php-and-mysql-date-function.html
but when I am viewing it in phpmyadmin I see the result:

02/11/09 1


My field has been set to type VARCHAR.


Anyone know how to do it?
__________________
Free iPhone | Free Facebook Likes
BuLLDoG is offline   Reply With Quote
Old 11-02-2009, 08:56 AM   #2 (permalink)
NamePros Regular
 
demaxxus's Avatar
Join Date: Jul 2007
Posts: 323
demaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to behold
 



If you're going to store your dates in the format YYYY-MM-DD you should probably use the date type instead of varchar. You'll find that you'll run into all sorts of problems using varchar if you're trying to query on those dates.

Also, depending on what type of data you're storing in this table, you may also want to think about using the datetime type, which will save the full date and time in the format YYYY-MM-DD HH:MM:SS.
demaxxus is offline   Reply With Quote
Old 11-02-2009, 09:11 AM THREAD STARTER               #3 (permalink)
NamePros Regular
 
BuLLDoG's Avatar
Join Date: Aug 2003
Location: England
Posts: 934
BuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of light
 



If I set the field to 'date' type, how should my sql statement look?

---------- Post added at 05:11 PM ---------- Previous post was at 05:05 PM ----------

Because, I have just changed field type to 'datetime'

And used the following code:

PHP Code:
$datestamp=date("y-m-d h:i:s"); //create date time
????: NamePros.com http://www.namepros.com/showthread.php?t=620615


$sql="INSERT INTO $tbl_name(itpname, detail, author, externalurl, datetime)VALUES('$itpname', '$detail', '$author', '$externalurl', $datestamp)"
and the result I see in phpmyadmin is now: 2002-11-09 11:08:36

I want it to be displayed as YYYY-MM-DD HH:MM:SS
but as you can see its put it in the wrong order?
__________________
Free iPhone | Free Facebook Likes
BuLLDoG is offline   Reply With Quote
Old 11-02-2009, 09:11 AM   #4 (permalink)
NamePros Regular
 
demaxxus's Avatar
Join Date: Jul 2007
Posts: 323
demaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to behold
 



Originally Posted by BuLLDoG View Post
If I set the field to 'date' type, how should my sql statement look?
curdate() already returns the date in the YYYY-MM-DD format, so your current query should work as is.
Last edited by demaxxus; 11-02-2009 at 09:21 AM.
demaxxus is offline   Reply With Quote
Old 11-02-2009, 09:15 AM THREAD STARTER               #5 (permalink)
NamePros Regular
 
BuLLDoG's Avatar
Join Date: Aug 2003
Location: England
Posts: 934
BuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of light
 



Even with curdate() its putting it as 2002-11-09 :S
__________________
Free iPhone | Free Facebook Likes
BuLLDoG is offline   Reply With Quote
Old 11-02-2009, 09:21 AM   #6 (permalink)
NamePros Regular
 
demaxxus's Avatar
Join Date: Jul 2007
Posts: 323
demaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to behold
 



For your question about datetime, try using the following format for your php date function - "Y-m-d H:i:s" (capital Y and H).

In other words, use this:

Code:
$datestamp=date("Y-m-d H:i:s");

$sql="INSERT INTO $tbl_name(itpname, detail, author, externalurl, datetime)VALUES('$itpname', '$detail', '$author', '$externalurl', '$datestamp')";
Last edited by demaxxus; 11-02-2009 at 09:30 AM.
demaxxus is offline   Reply With Quote
Old 11-02-2009, 09:21 AM THREAD STARTER               #7 (permalink)
NamePros Regular
 
BuLLDoG's Avatar
Join Date: Aug 2003
Location: England
Posts: 934
BuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of light
 



Same problem is happening...
__________________
Free iPhone | Free Facebook Likes
BuLLDoG is offline   Reply With Quote
Old 11-02-2009, 09:29 AM   #8 (permalink)
NamePros Regular
 
demaxxus's Avatar
Join Date: Jul 2007
Posts: 323
demaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to behold
 



Now that you're using the php date function and then inserting the variable into the db, instead of running curtime, you'll have to put ' ' around the $datestamp variable when inserting.
demaxxus is offline   Reply With Quote
Old 11-02-2009, 09:37 AM THREAD STARTER               #9 (permalink)
NamePros Regular
 
BuLLDoG's Avatar
Join Date: Aug 2003
Location: England
Posts: 934
BuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of light
 



Yup, it has one but still giving the same problem? quite strange I didn't think dates would be this problematic
__________________
Free iPhone | Free Facebook Likes
BuLLDoG is offline   Reply With Quote
Old 11-02-2009, 09:45 AM   #10 (permalink)
NamePros Regular
 
demaxxus's Avatar
Join Date: Jul 2007
Posts: 323
demaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to behold
 



That's really bizarre, it should be working. Try narrowing it down first...

Replace:
Code:
$datestamp=date("Y-m-d H:i:s");
With:
????: NamePros.com http://www.namepros.com/showthread.php?t=620615
Code:
$datestamp=date("Y-m-d H:i:s"); 
echo $datestamp; 
exit;
When you load that page in your browser is it displaying the date and time correctly?
demaxxus is offline   Reply With Quote
Old 11-02-2009, 09:59 AM THREAD STARTER               #11 (permalink)
NamePros Regular
 
BuLLDoG's Avatar
Join Date: Aug 2003
Location: England
Posts: 934
BuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of light
 



it works whilst I echo it out, just when it gets added to the database I hit the problem..
__________________
Free iPhone | Free Facebook Likes
BuLLDoG is offline   Reply With Quote
Old 11-02-2009, 10:02 AM   #12 (permalink)
NamePros Regular
 
demaxxus's Avatar
Join Date: Jul 2007
Posts: 323
demaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to beholddemaxxus is a splendid one to behold
 



Try the same with the insert statement.

Code:
$sql="INSERT INTO $tbl_name(itpname, detail, author, externalurl, datetime) VALUES ('$itpname', '$detail', '$author', '$externalurl', '$datestamp')";
echo $sql;
exit;
If that's displaying properly, and the table field is set to use the datetime type, I'm not really sure what to tell you, the code you have should be working.
demaxxus is offline   Reply With Quote
Old 11-02-2009, 10:11 AM THREAD STARTER               #13 (permalink)
NamePros Regular
 
BuLLDoG's Avatar
Join Date: Aug 2003
Location: England
Posts: 934
BuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of lightBuLLDoG is a glorious beacon of light
 



Ah school boy error, it is working now, the type was date instead of datetime my bad. Thanks ever so much for your help demaxxus
__________________
Free iPhone | Free Facebook Likes
BuLLDoG is offline   Reply With Quote
Old 11-02-2009, 10:48 AM   #14 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,963
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
Instead of formatting the date in PHP you can also do it with mySQL using the DATE_FORMAT function.
__________________
NameNewsletter.com - free lists of available domain names
ZoneFiles.net (beta) - ccTLD and gTLD droplists
sdsinc is offline   Reply With Quote
Old 11-02-2009, 05:14 PM   #15 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



i believe the mysql NOW() function also does the same thing to get datetime.

i personally always just keep it in terms of epoch time and then use php functions like date and mktime to go back and forth between epoch and readable time. it's MUCH easier to do computations with epoch time, so if you'll be doing things like time between datetimes etc, i'd suggest looking into it.
nasaboy007 is offline   Reply With Quote
Reply


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


 
All times are GMT -7. The time now is 02:32 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger