Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: sheldonlg Newsgroups: comp.lang.php Subject: Re: [newbie] datetime issues Date: Wed, 08 Jun 2011 20:12:50 -0400 Organization: A noiseless patient Spider Lines: 35 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 9 Jun 2011 00:14:29 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="UEuj9QXnmO1yVSvM2qfkZA"; logging-data="8431"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX189lZmrszCLTWLLKiBa5gypFel+Jq3q7DA=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: Cancel-Lock: sha1:Xqw8j26VH/0wWfPxX67wXZHNl7U= Xref: x330-a1.tempe.blueboxinc.net comp.lang.php:2034 On 6/8/2011 6:54 PM, Jeff wrote: > hi > > php 5 > > I'm trying to construct an sql query where I need to filter between 2 dates. > So how do I dynamically create those datetime values? > > Tip: The day value will always be 1 (or 01 ?). > > I will dynamically select values from the entire last month. > > I tryed this: > convert('2012-'.date('m').'-01 00:00:00',DATETIME) > which gave me syntax error and also I need to some more if testing on year. > So hope there is a better way of doing this > > please help me What do you mean by "from the entire last month"? The expression you have will give for June, for example, '2012-06-01 00:00:00' which is already in DATETIME format (which I assume is how your data are stored in that column). So, if you want everything from the first of June it would be $sql ="SELECT WHATEVER FROM WHEREVER "; $sql .= "WHERE THE_COL_NAME >= '2012-" . date('m') . "-01 00:00:00'"; Note that the final value of $sql in this case, for June, would be SELECT WHATEVER FROM WHEREVER WHERE THE_COL_NAME >= '2012-06-01 00:00:00' -- Shelly