Solved

Looking between dates in my db.

Posted on 2011-02-17
5
199 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
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:dsmile
Comment Utility
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
Comment Utility
woops... yes i do lol
0
 
LVL 13

Expert Comment

by:dsmile
Comment Utility
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
Comment Utility
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
Comment Utility
thats WELL easy to the way i am doing it...thanks :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now