I'm trying to compare 2 dates with an SQL Query,
My query is this :
"SELECT table.toto
FROM table
WHERE
table.date < $date"
$date is a variable that comes from my PHP code
In my table, my date is formatted like this : dd/mm/YYYY
Unfortunately, it doesn't work as expected, it returns only one result (it's supposed to return way more results), which date is : 1312-09-15 00:00:00 but in database, the date is formatted like dd/mm/YYYY.
Well, I found a solution to my problem.
I don't know why but I just have set the "$date" format to : n/d/Y
and to wrap it between "#" in code like this :
"SELECT table.toto
FROM table
WHERE
table.date < #date#"
Related
I have a calendar in html form and I want to insert this date into MySQL. The default MySQL date is 0000-00-00. But in my country the format is DD/MM/YYYY. So what to do to fix it. Thank you. I am using PHP.
You must use one format in your HTML page, and another format in your database.
So, if you want to store a date like this '12/05/2008' into mySql, you must transform it like this:
$date = '12/05/2008';
$dateToStore = date('Y-m-d', strtotime(str_replace('/','-',$date)));
And if you wonder why, you need to replace the '/' with '-' to make php know that the first part of the data string is the day, and then the month (as I think is your case).
MySQL the date format is always YYYY-MM-DD. To convert it to another format, you need to manually convert the retrieved date to the desired format like
$displayDate=date("d/M/Y", strtotime($mysqldate));
Method 1
You cant insert into DD/MM/YYYY format. Instead while rendering it in view file you can change into desired format.
<?php
$date = $result['db_date']; // I ASSUMED YOUR DB FIELD IS db_date
$desiredFormat = date('d/m/Y', strtotime($date)); // CONVERTING INTO YOUR FORMAT
echo '<pre>'; print_r($desiredFormat); // DISPLAYING IT
?>
Method 2
You can retrieve from database in your desired format using below
SELECT *, DATE_FORMAT(YOUR_DATE_FIELD, "%m/%d/%Y") AS date FROM YOUR_TABLE;
Use MySQL STR_TO_DATE
Try this mysql query :-
INSERT INTO `table`(`date`) VALUES (STR_TO_DATE('10/10/2015', '%d/%m/%Y'))
In my database field there in date and time saved using time(); function.
I want to compare that one with given date in date format like '20/02/2015 '
my query is like:
select view,optin,insertTime,isUnique from sg_page_report as PR where insertTime='20/02/2015'"
where insertTime contains different format date like '1393587636'
How can I resolve this one?
WHERE FROM_UNIXTIME(insertTime)='2015-02-20'
Or you can do it like
$ts=mktime(0,0,0,2,20,2015); // or
//$ts=strtotime('20/02/2015'); // inefficient
And your query can be modified to look like
WHERE insertTime=$ts
I want to convert the data on which I have the format
$dateToday = date("d-m-Y");
so the value of $dateToday is 27-12-2012
Then I want to save it to the database with the mysql data type date. How to keep the value of 27-12-2012 it can be stored in the mysql database with the format 2012-12-27?
Help me please. Thank you
Yes, you can convert the date with strtotime();
$dateToday = date("d-m-Y");
$newDate = date("Y-m-d", strtotime($dateToday));
OUTPUT: 2012-12-27
And then you can store data to your database.
When you have to recover the date you can reverse this operation like this:
$dateFromDatabase = "2012-12-27";
$reverseDate = date("d-m-Y", strtotime($dateFromDatabase));
OUTPUT: 27-12-2012
(corrected "Y-m-d" to "d-m-Y" in 2nd date call)
this is how it works:
You have to store your data in the proper mysql format. It will allow you to make whatever ordering, aggregating, filtering and calculating your dates.
But when you need to display your data, you may convert it in whatever format you wish, using mysql DATE_FORMAT() function:
SELECT DATE_FORMAT(dt,'%d-%m-%Y') as dtf FROM TABLE
will give you dtf field formatted in your custom format
i'll show u how to do that.
To explain i create one table called testtable1 it contain only one column called
col1 of type DATE
Table creation query is given below
CREATE TABLE `testtable1` (
`col1` DATE NULL DEFAULT NULL
)
Following query will work as you need.
In the first line i declared a string variable. In the second line i converted that string to your required date format and inserted into table.
set #var1='27-12-2012';
insert into testtable1 values(STR_TO_DATE(#var1, '%d-%m-%Y'))
You could also try to use the mysql function for converting to a date from a string, i.e
STR_TO_TIME
.
The SQL query could be
INSERT INTO foo_table (foo_date)
VALUES (STR_TO_DATE('27-12-2012','%d,%m,%Y'))
If you want to store data in MYSQL table in this format, you need to declare the column as varchar.
Because the datetime store date in a different format like 'yyyy-mm-dd hh:mm:ss'
The output is wrong This cannot show the date from the database .This show 1970/01/01.....
$date=Date($year."/". $month."/". $day);
$date=Date("Y-m-d", strtotime($date));
echo $date;enter code here
Try this
$dateToday = date("d-m-Y");
$dateForMysql = date('Y-m-d', $dateToday));
How would i go about checking to see if an auction has expired in my database? I have a datetime column in MySQL that i believe is of the following format: YYY-MM-DD hh:mm:ss. If this is the case would the following check work - i.e. want to select only expired auctions from the table in the database...
<?php
//Some code
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ($date_time > finish_time)
");
?>
While "finish time" is a column in the database of the above cited format. Presuming this works how actually do i get the current date into the same format? If anybody knows i would very grateful cheers. Even more so if the above query wouldn't work and something else is required. Thanks again.
Oh and of course i would define the date_time variable to start with
Do you actually need the $date_time variable? The easiest way to do this would be SELECT * FROM auction WHERE finish_time < NOW(). That way you'll get your results and don't have to set the date from PHP.
You can use
<?php
//Some code
$date_time=strtotime($date_time);
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ( $date_time > UNIX_TIMESTAMP(finish_time))
");
?>
This solution:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
is from this post: PHP Zend date format
and here's another solution: how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript?
and to do it in php:
How to get the current date and time in PHP?
The function date is made for it:
// Get the current date
$date_time = date("y-m-d H:i:s");
Look at the documentation page to see other format flags ;)
im storing the timestamp in mysql database in (INT) column, And i want to search the rows with between the dates. Anyone would please help what should be the Sql query to find the rows between two dates?
dates are entered like
FROM DATE = 15-10-2011
END DATE = 01-11-2011
It depends on what algorithm you use to convert the date strings to int values.
If the algoritm is mototonic, for example: If a day (say 15-10-2011) is converted to n (say 5037), then the next day (16-10-2011) is always converted to n+1 (so 5038 in this example.)
then you could just use:
WHERE IntField BETWEEN MySpecialConvertDateToIntFunction('15-10-2011')
AND MySpecialConvertDateToIntFunction('01-11-2011')
If your field stores different timsetamps as different integers (and the conversion is monotonic), you could change the above code slightly to:
WHERE IntField >= MySpecial...Function('15-10-2011')
AND IntField < MySpecial...Function('02-11-2011') --- notice the date+1
But it's usually better to use a field of the MySQL DATE type for storing dates. Unless you want to store dates before 1000 or after 9999 off course.
If you want to store timestamps, there's also a TIMESTAMP type. Read the
MySQL docs: DATETIME, DATE, and TIMESTAMP Types
You can use The BETWEEN operator, which selects a range of data between two values. The values can be numbers, text, or dates.
You can see there:
http://w3schools.com/sql/sql_between.asp
I would ask you to set data type as timestamp/datestamp & then
//php code
$date1=date ('Y-m-d' , strtotime ( "15-10-2011") );
$date2=date ('Y-m-d' , strtotime ( "01-11-2011") );
//sql code
SELECT * FROM tbl_mytbl WHERE DATE(attr_date) <'$date2' AND DATE(attr_date) >'$date1'
Can you use the mysql FROM_UNIXTIME function dev.mysql.com - function from_unixtime
SELECT *
FROM 'table'
WHERE FROM_UNIXTIME(intTimestamp)
BETWEEN
date ('Y-m-d' , strtotime ( '15-10-2011') )
AND ('Y-m-d' , strtotime ( '01-11-2011'));
I had made a mistake with the date input but have fixed.