I stored expiry date field as var char datatype in MySQL table. Now i need to compare expiry date with current date. How to convert var char datatype as date in php coding and compare date in select query where condition.
why don't you do it in the query itself.
$current_date=date("Y-m-d");
mysql_query("select * from your_table where STR_TO_DATE(expiry_date,'%Y-%M-%D')>'".$date."');
or
You can do it in php itself
if(strtotime($current_date) > strtotime($row['date'])) {
//your code
}
This works for me:
SELECT * FROM table_name WHERE start_date <= CURDATE() AND end_date >= CURDATE()
Related
I want to fetch and display all the record where the document date is below the date selected. Let says i pick a date from date picker 2016/09/28, means it will display all the record which is below the selected date.
SELECT * From `bill` WHERE bill_date <= '".$from."'
Try something like this:
$selDate = date('Y-m-d', strtotime($from));
// convert the selected date to mysql date format
SELECT * From `bill` WHERE bill_date <= '".$selDate."';
Note: If the bill_date is of type datetime than extract date portion using date() function and than put the condition on it.
I have a table with a column DateFrom (Varchar) that uses format YYYY-MM-DDTHH:MM:SS
to get the time for a specific date I use this sql:
SELECT SUBSTRING(DateFrom FROM 12 FOR 5) AS From FROM calendar WHERE SUBSTRING(DateFrom FROM 1 FOR 10) = "'.$date.'"
this gives me:
HH:MM
.$date
(from datepicker) has format DD.MM.YYYY
I'm creating a website that should let the user see times associated with a date
can I convert the string from my database before checking if it's equal to .$date?
Or would it be better to change format from datepicker to YYYY.MM.DD instead?
How about getting rid of the the substring stuff and using this instead?
SELECT TIME(DateFrom) AS `From`
FROM Calendar
WHERE DATE(DateFrom) = STR_TO_DATE($date,'%d.%m.%Y')
This will work because your DateFrom field happens to be stored in the standard string format of a DATETIME object.
From is in backticks because it's a reserved word.
You really should consider changing the data type of your DateFrom column to DATETIME, and then indexing that column. Then you can change your WHERE clause to this
WHERE DateFrom >= STR_TO_DATE($date,'%d.%m.%Y')
AND DateFrom < STR_TO_DATE($date,'%d.%m.%Y') + INTERVAL 1 DAY
and you will get a very great performance advantage.
This is how i finally solved it:
$date1 = 18.02.2014;
$date2 = 22.02.2014;
$a = explode('.',$date1);
$revdate1 = $a[2].'-'.$a[1].'-'.$a[0];
$newdate1 = str_replace(".","-",$revdate1);
$b = explode('.',$date2);
$revdate2 = $b[2].'-'.$b[1].'-'.$b[0];
$newdate2 = str_replace(".","-",$revdate2);
SELECT SUBSTRING(DateFrom FROM 1 FOR 10) AS Date,
SUBSTRING(DateFrom FROM 12 FOR 5) AS StartTime,
SUBSTRING(DateTo FROM 12 FOR 5) AS EndTime
FROM Calendar WHERE SUBSTRING(DateFrom FROM 1 FOR 10)
BETWEEN "'.$newdate1.'" AND "'.$newdate2.'"
I know DATETIME would be better but that was not an option here (not my database).
So this solution takes a date with format dd.mm.yyyy and reformats it to yyyy-mm-dd and checks it against the substring date from my table.
so the query prints all:
Date, StartTime, EndTime
between two dates
I have row in which is string value from strtotime(), for example 1303448400.
My table has following stucture:
id | date
And from my input I recive date in this format: MM/DD/YY.
How to create a query in SQL which will select id where date is greater than 11/11/13?
You could convert your date (11/11/13) to a timestamp before using it in a query, using mktime():
http://php.net/manual/en/function.mktime.php
Use strtotime as you said,
SELECT * FROM TABLE WHERE DATE > strtotime($yourDate)
$date=strtotime('11/11/13');
SELECT * FROM TABLE db_date DATE > $date
This is how it will work for you.
$date_1 = 11/11/13; // your date
$date_2 = strtotime($date_1); //change it to strtotime
$query = mysql_query("SELECT id FROM table WHERE date DATE > '".$date_2."'"); //select id which is greater
Change the variables and rows to fit yours.
Would this be possible? I've used this to insert the date into a field called "date":
$date=date("m/d/y");
$sql="INSERT INTO pool (date) VALUES('$date' )";
$result=mysql_query($sql);
I've used this statement to get the date a week ago:
$variable = date('d-m-y', strtotime('-1 week'));
So how would I SELECT any rows which were added last week?
Instead of storing your dates as m/d/y, you should store them as Y-m-d :
$date=date("Y-m-d");
$sql="INSERT INTO pool (date) VALUES('$date' )";
In the database, you dates will then look like 2011-04-09.
That format is much easier to work with : alphabetical comparisons will work.
Which means that searching for rows that are older than a certain date would become something like this :
$variable = date('Y-m-d', strtotime('-1 week'));
$query = "select * from pool where date < '$variable'";
Also note that instead of working with a date field which is a varchar (or an equivalent) in your database, you could use a DATE column -- which would allow to to work with date and time functions in MySQL.
If the date field is a proper date type you can do < or > in your sql query. For example -
SELECT * FROM table WHERE date > '$date'
If you want everything from 1 week ago to now you can do something like the above or
SELECT * FROM table WHERE date BETWEEN '$date' AND NOW()
I have the field datetime with DATETIME type in MySQL. In PHP script I set date begin and date end like this: 11/12/1999 and 11/12/2001. In my table datetime saved in the next format: 11.11.1888 00:00:00. How can I compare these dates?
Thanks.
It could be done more easly, but this is one way:
$begindate = strreplace($begindate,'/','.');
$enddate = strreplace($enddate,'/','.');
mysql_query("SELECT * FROM MyTable
WHERE dateField BETWEEN '$begindate 00:00:00' AND '$enddate 23:59:59'");
should be something like
SELECT *, STR_TO_DATE(datetime,'%d.%m.%Y') as newdatetime
FROM table
WHERE newdatetime >= STR_TO_DATE('11/12/1999','%d/%m/%Y')
AND newdatetime <= STR_TO_DATE('11/12/2001','%d/%m/%Y');