Jump to content

WHERE day, month, year from datetime column


lukep11a

Recommended Posts

Does anybody know how I can select the day, month and year from the variable $date which is stored as datetime. I am trying to display all fixtures for each day that there are fixtures for. I am doing it like this as some fixtures have the same date but different kick off times.

 

So, I was thinking something like WHERE Month(fixtures.date) = '$date' AND Day(fixtures.date) = '$date' but it's not returning any results. Here is my code:

 

$query1="SELECT home.team_name AS home_team, away.team_name AS away_team, competitions.comp_name
			FROM fixtures
			LEFT JOIN teams home
			ON fixtures.ht_id = home.team_id
			LEFT JOIN teams away
			ON fixtures.at_id = away.team_id
			LEFT JOIN competitions
			ON fixtures.comp_id = competitions.comp_id
			WHERE Month(fixtures.date) = '$date' AND Day(fixtures.date) = '$date'
			GROUP BY fixtures.fixture_id
			ORDER BY fixtures.date ASC";

Link to comment
Share on other sites

$date is selected from fixtures.date column and could contain

 

2012-07-27 15:00:00

 

so i want to select all fixtures that are on 27 July, ie:

 

2012-07-27 15:00:00

2012-07-27 18:00:00

2012-07-27 21:00:00

 

and not just the ones that are exactly equal to 2012-07-27 15:00:00 as some fixtures have different kick off times, if that makes sense?

Link to comment
Share on other sites

Thanks Barand, $date is defined by $row['date'] which is from the fixtures table where the row is set as datetime format, it is set in this format so that it includes the date of the fixture and the kick off time. As not all fixtures kick off at the same time I need to find a way to show all fixtures from any day that there are games scheduled.

Link to comment
Share on other sites

Look at your comparisons. You're trying to compare the $date variable in YYYY-MM-DD hh:mm:ss format against MONTH(fixtures.date) and also against DAY(fixtures.date). Next, look at the values returned by those two MySQL functions, and you'll see why using the variable $date will not work.

 

DAY()

MONTH()

Link to comment
Share on other sites

Do you know a way that I can make it work?

 

I've converted $date to $date = date('Y-m-d', strtotime($row['date']));

 

then I though something like this in the WHERE statement to convert fixtures.date into yyyy-mm-dd, WHERE CONVERT(CHAR(10),fixtures.date,120) = '$date' but can't seem to get it working

Link to comment
Share on other sites

Thanks Barand, $date is defined by $row['date'] which is from the fixtures table where the row is set as datetime format, it is set in this format so that it includes the date of the fixture and the kick off time. As not all fixtures kick off at the same time I need to find a way to show all fixtures from any day that there are games scheduled.

 

DATE(fixture.date) will return the date only part of of the datetime field and ignore the time portion. This enables you to compare with another date, such as I gave you in my earlier example.

Link to comment
Share on other sites

I'm really sorry Barand. I didn't read your first reply properly. DATE(fixtures.date) was exactly what I was looking for, it's now working. I didn't expect it to be that simple especially after reading up on SQL date/time functions. There are so many I didn't really know where to start!!

 

Thanks for your help... again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.