Solved

Looking between dates in my db.

Posted on 2011-02-17
5
205 Views
Last Modified: 2012-05-11
$firstOfMonth = date("Y-m-d", strtotime('01/'.date('m').'/'.date('Y').'00:00:00'));
$lastOfMonth = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime('01/'.date('m').'/'.date('Y').'00:00:00'))));
echo $firstOfMonth.'....'.$lastOfMonth;
 $getuserprofile =  mysql_query("SELECT * from race12 WHERE ev_dat between '$firstOfMonth' and '$lastOfMonth'");
 $num_rows = mysql_num_rows( $getuserprofile);

Open in new window



Im getting no results even though there are 2 dates with 2011-02-10

and when i echo
echo $firstOfMonth.'....'.$lastOfMonth;

Open in new window


i get

2011-01-02....2011-02-01 when it should be 2011-01-01....2011-01-lastday
0
Comment
Question by:runnerjp2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:dsmile
ID: 34914675
2011-01-02....2011-02-01 when it should be 2011-01-01....2011-01-lastday

--> do you mean

2011-01-02....2011-02-01 when it should be 2011-02-01....2011-02-lastday
0
 

Author Comment

by:runnerjp2005
ID: 34914686
woops... yes i do lol
0
 
LVL 13

Expert Comment

by:dsmile
ID: 34914721
If that was the case then the part of preparing input date should be like this

$firstOfMonth = date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
$lastOfMonth = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));

What is type of ev_dat?
You query will only work if its type is DATE
0
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 34914735
BTW, you don't have to create exact last date.
To fulfill your requirement of select data from first to last date of the month, you can easily (and safely)  use this

$firstOfMonth = date("Y-m-01");
$lastOfMonth = date("Y-m-31");

Cheers
0
 

Author Closing Comment

by:runnerjp2005
ID: 34914757
thats WELL easy to the way i am doing it...thanks :)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question