hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Mysql query to get day, month, and year?
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Mysql query to get day, month, and year?

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 03-26-2010, 06:18 AM
lexington lexington is offline
Disabled
 
Join Date: Dec 2003
Posts: 1,941

Mysql query to get day, month, and year?


Hello, I have always used php to get the current day, month, and year from the database. I heard that using a mysql query instead is faster. Here is what I have:

PHP Code:
"UPDATE stats_table SET rank = '".$rank."'
                             WHERE song_id = '"
.$rank_row['song_id']."' 
                             AND day = '"
.date("j")."'
                             AND month = '"
.date("n")."'
                             AND year = '"
.date("Y")."'" 
You can see where I used the date() functions. Could you tell me how to make do the same thing using mysql to get the current day, month, and year? Basically the same query you see above without php generating the date. It cannot be NOW() or anything, it has to be separate rows for the date like I have posted. Thanks.

Reply With Quote


Sponsored Links
  #2  
Old 03-26-2010, 07:24 AM
marrtins marrtins is offline
Junior Guru Wannabe
 
Join Date: May 2009
Location: Latvia
Posts: 71
You can use EXTRACT() function. Please check out: http://dev.mysql.com/doc/refman/5.1/...nction_extract

Reply With Quote
  #3  
Old 03-26-2010, 07:42 AM
lexington lexington is offline
Disabled
 
Join Date: Dec 2003
Posts: 1,941
Thanks, but I am having a hard time understanding which one to use for mysql to get the separate day, month, and year without inputting the date value. Basically looking for the equivalent to date() in php but in a mysql format.


Last edited by lexington; 03-26-2010 at 07:45 AM.
Reply With Quote
Sponsored Links
  #4  
Old 03-26-2010, 07:50 AM
marrtins marrtins is offline
Junior Guru Wannabe
 
Join Date: May 2009
Location: Latvia
Posts: 71
I believe it won't have any noticeable impact on performance, unless you do this query billions times.

If you want to improve performance, I would rather start with optimizing overall application design than with such micro optimizations.

Reply With Quote
  #5  
Old 03-26-2010, 01:56 PM
mattle mattle is offline
Web Hosting Master
 
Join Date: May 2009
Posts: 766
Um...personally, I'd look at the following MySQL functions:

DAY(), MONTH(), YEAR()

Reply With Quote
  #6  
Old 03-26-2010, 03:09 PM
lexington lexington is offline
Disabled
 
Join Date: Dec 2003
Posts: 1,941
Quote:
Originally Posted by mattle View Post
Um...personally, I'd look at the following MySQL functions:

DAY(), MONTH(), YEAR()
Yeah but I must be doing it wrong since if I use:

SELECT * FROM stats_table where day = DAY()

It errors. What would be the correct query please? Thanks.

Reply With Quote
  #7  
Old 03-26-2010, 04:30 PM
mattle mattle is offline
Web Hosting Master
 
Join Date: May 2009
Posts: 766
All of those functions take a date argument, just like the PHP date() function does--except there are no default values in MySQL functions. To get the current day, for example, you'd use DAY(NOW()).

http://dev.mysql.com/doc/refman/5.1/...functions.html

Reply With Quote
  #8  
Old 03-26-2010, 08:10 PM
lexington lexington is offline
Disabled
 
Join Date: Dec 2003
Posts: 1,941
That is exactly what I needed thanks So you guys can confirm that using DAY(NOW()) is slightly faster (even if noticeable) than using a php date() function to display the current day?

Reply With Quote
  #9  
Old 03-26-2010, 09:06 PM
tim2718281 tim2718281 is offline
Web Hosting Master
 
Join Date: Mar 2009
Posts: 2,218
Quote:
Originally Posted by lexington View Post
That is exactly what I needed thanks So you guys can confirm that using DAY(NOW()) is slightly faster (even if noticeable) than using a php date() function to display the current day?
No, it will be slower.

But why don't you just time it and see?

Reply With Quote
  #10  
Old 03-29-2010, 08:38 AM
mattle mattle is offline
Web Hosting Master
 
Join Date: May 2009
Posts: 766
Quote:
Originally Posted by tim2718281 View Post
No, it will be slower.

But why don't you just time it and see?
I don't know that you can say that unequivocally. Certainly, this would be faster:

PHP Code:
$date date("j"); 
than

PHP Code:
$date mysql_result(mysql_query("SELECT DAY(NOW()) AS date"0"date")); 
However, the following two approaches will be dependent on server hardware, and process priority:

PHP Code:
mysql_query("UPDATE table SET day=" date("j") . " WHERE id=1");
// vs
mysql_query("UPDATE table SET day=DAY(NOW()) WHERE id=1"); 
It's oft claimed here that php can do internal date calculations faster than MySQL can, but I've seen zero evidence to support such claims.

Reply With Quote
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared from $15/Year || Resellers from $4/Month || Est. 2004 || 27 Day MBG Rydel Shared Hosting Offers 0 01-15-2006 03:20 PM
User Input => Day/Month/Year seodevhead Programming Discussion 5 10-29-2005 12:16 PM
Mysql-delete a database query in 1 day intervals All-Starr 24-7 Programming Discussion 4 06-30-2004 03:32 PM
Every day week month year with your server cannibal Dedicated Server 8 12-20-2003 06:00 AM
IHosting $40/YEAR-$65/YEAR 500MB-3GB Webspace 12GB-25GB Bandwidth/Month PHP/MySQL!@! Voldemort Shared Hosting Offers 4 08-18-2002 11:42 AM

Related posts from TheWhir.com
Title Type Date Posted
Web Hosting Sales and Promos Roundup – May 17, 2013 Web Hosting News 2013-05-17 15:06:04
Web Hosting Sales and Promos Roundup – February 8, 2013 Web Hosting News 2013-02-08 16:47:52
Web Hosting Sales and Promos Roundup – May 18, 2012 Web Hosting News 2012-05-18 17:05:02
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Web Host 1&1 Internet UK Adds Business Hosting Plan, Cloud Server Features Web Hosting News 2011-09-13 15:50:08


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?