We help IT Professionals succeed at work.

how can i compare dates in php

amchugh89
amchugh89 asked
on
i have a javascript table that is populated from a tadabase table called "games" with the following query:

$query1="select * from games";
...and then some inserting into the js table

one of the table columns i have is the game_date.
i want to get it so that games that have already happened are no longer in my js table
i want a query something like:
$query1="select * from games WHERE current_date > game_date";

does my game_date have to be of time type?
does it need to be in a specific format?
is there a php function that gets the current date?
how should i go about comparing these 2 dates - i'm a total n00b
Comment
Watch Question

Web Architect
Commented:
you can do the query you said, will work, date format must be YYYY/mm/dd and you should put it betwen quotes...

for php current date just use $curdate = date('Y/m/d');

if you just wont to compare the dates just do like this


example you want to select only gamedate that is greater or equal to today

just do where yourField >= '$date'


cheers
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Your dates have to be in the format that you find them in the database.  MySQL does help you out if the fields are declared as "DATE" type.  If they are TEXT or VARCHAR, then you will need to get the dates into PHP and process them into a form that can be used for a comparison.  Can you post the table structure so we can see what you have to work with?
Commented:
as I answered in the duplicate of this question, I suggest to put the filter into your SQL script.

So current date can replaced by GETDATE(), NOW(), depending on your SQL server. You can search for date / time functions in your SQL manual...
Commented:
I would suggest reading this article to get familiar with dates in PHP.

http://www.phpfreaks.com/tutorial/working-with-dates-in-php
Commented:
For this example, I am assuming that you only want games played today. If you want to limit it by time as well, we can just add the time, but that depends if time is specified in your database
If your dates in your database are in standard mySQL format (YYYY-mm-dd) then
$now=date("Y-m-d");
$yesterday=date("Y-m-d", strtotime($now."-1 day"));
 $query1="select * from games WHERE game_date>'$yesterday' "; // note the single quotes around $yesterday
If the date in the database is in some other format (but still a string) your query should become, if the date was in your database as "06,11,2010"
$query1="select * from games WHERE STR_TO_DATE(game_date, '%d,%m,%Y')>'$yesterday' ";  
You have to tell STR_TO_DATE the format of the non standard date data using the chart found at http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
 
If you are using the MySQL DB and game_date field is of date type then try the following query,

SELECT * FROM `games` WHERE game_date >= CURDATE( )
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.