GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jan-2007, 23:24
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Select data by date


hi all, i have a simple system that include function of selecting data from dbase by month. in my table date was set as format Y-m-d
so now, how can i write query to select only by month.. is it possible if i write query as
PHP Code:

SELECT * FROM terima WHERE date_in = '%%%%-o1-%%' 



i've run it but didnt work
  #2  
Old 11-Jan-2007, 02:23
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: select data by date


When you are trying to search by MONTH, shouldn't you care about the YEAR too?

I mean, if you had 2 records for January, but one was for 2006 and the other for 2007, how would that work?

If the date_in column is of the date type, then I suppose one way to do this would be something like:

Code:
SELECT * FROM `terima` WHERE '2007'=YEAR(`date_in`) AND '1'=MONTH(`date_in`)

Sorry ctrohaya,

I don't know how you managed to get online to post this question since I am also using TMNet and we both know how ridiculous the connection has been recently. Today is good though... so I am catching up.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #3  
Old 12-Jan-2007, 20:04
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: select data by date


thanks a lot for giving reply to my question, actually it was settled. it is true of using month and year. i just ask about month, it mean if i got the solution for month, then i'll also got solution about year.
now, may i ask you on how to convert seconds to date(Y-m-d)?
i already know how to convert date to seconds, but how to re-convert seconds to date.
i want to know what is the date if the seconds is 2592000?
  #4  
Old 12-Jan-2007, 20:23
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: select data by date


When you say "seconds", do you mean the unix timestamp? The number of seconds since Jan. 1, 1970?

If so, like this:

PHP Code:

<?php
$uts = 2592000;
echo "Y-m-d is ", date( "Y-m-d", $uts );

// Result:
// Y-m-d is 1970-01-31
?>


You put the "seconds" in the second parameter of the date() function.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #5  
Old 12-Jan-2007, 21:54
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: select data by date


it already looks close to my question. the number of seconds since current date.
for ex. if customer enter 30days, we will find the date 30 days backward from current date.
  #6  
Old 12-Jan-2007, 22:29
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Calculate date, 30 days ago


I will try one last time. You should really put some effort into your questions as well.

We discussed strtotime() before, so refer to the thread if you need to know how to get a unix timestamp relative to a date or the system's date.

As for your question, see if this is what you were expecting:

PHP Code:

<?php
$days = 30; // 30 days ago

// get the unix timestamp, 30 days ago.
$uts_offset = strtotime( "-$days days" );

// format the timestamp into 'Y-m-d'
echo "$days days ago was ", date( "Y-m-d", $uts_offset );

// Result:
// 30 days ago was 2006-12-14
?>

__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #7  
Old 13-Jan-2007, 02:15
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: Calculate date, 30 days ago


actually i've make my own effort based on your first post of how to convert second to date, this is my own script
PHP Code:

$period = 30 days ago
$today = date ("Y-m-d");
$second = $period *86400;
if (!$using_timestamps) {
$date_2 = strtotime($today, 0);
$date_1 = $date_2 - $second; //second
$tarikh = date( "Y-m-d", $date_1);
echo $tarikh; 
} 


and i'm so happy..... this script is work!
thank you so much for helping me find out the solution.......
  #8  
Old 13-Jan-2007, 02:56
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: Calculate date, 30 days ago


Many lines of unnecessary code and calculations. You can do it all with just date() and strtotime(), like this:

PHP Code:

<?php
$period = 30; // 30 days ago

if( !$using_timestamps )
{
    $tarikh = date( 'Y-m-d', strtotime("-$period days") );
    echo $tarikh;
}
?>

__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #9  
Old 17-Jan-2007, 19:53
ctrohaya ctrohaya is offline
New Member
 
Join Date: Nov 2006
Posts: 29
ctrohaya is on a distinguished road

Re: Select data by date


i have one more situation using date.
if i issue certain date for ex 2007-01-14 then i want to select from database the data that have same that and also date before such given date, that mean all data dated less than 14 will be shown($date<=2007-01-14)
is it possible if i use this script..
PHP Code:

SELECT * FROM terima WHERE  date_in LESS THAN '2007-01-14' 


???
  #10  
Old 18-Jan-2007, 01:26
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 841
admin will become famous soon enough

Re: Select data by date


Try:

Code:
SELECT * FROM `terima` WHERE `date_in`<='2007-01-14'
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Strange C++ code memory leakage problem gaoanyu C++ Forum 7 04-Nov-2005 09:09
Mrs stacy12 C Programming Language 14 05-Feb-2005 19:02
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.