Display Selected Month Values in mysql PHP - php

In MYSQL database, I have the date column which contains date in format [29-11-2012].
I would like to display the values based upon month selected by user via php page.
For Example, If user selects OCT then this should display all the other columns for this particular month.
Can you suggest SQL Query for this?
Thanks,
Shail

You can use
WHERE MONTH(STR_TO_DATE(formdate,'%d-%m-%Y'))
like this condition
Basically mysql stores dates in yyyy-mm-dd format only..

Related

How to compare date inserted from textbox with date from MYSQL db

I want to compare date inserted in textbox with date from MYSQL database.
My idea is to make car rent form (with FROM date - TO DATE), and after submiting that form, query will check if it is possible to rent that specific car in that period. Anyone had anything similar?
Thanks
We run many queries that are Start and End Date dependent.
SELECT * FROM rentCar
WHERE textBox BETWEEN StartDate AND EndDate

How to select data by using range of dates in MySql query?

I have a problem related to MySQL query, I use WAMPServer.
I have data in database which have range of dates but when I select data for example
select * from CHD WHERE addtime>='2018-06-15' and addtime<='2018-06-21';
It displays data from '2018-06-15' to '2018-06-20', data of 2018-06-21 are not displayed even if I do
select * from CHD where addtime='2018-06-21';
is not working
Please anyone can help me
This assumes that your column is of type datetime.
The shorthand version of your date in the filter clause is assumed to be at midnight of the date. Your values that you are attempting to retrieve have times after midnight of that date. You either need to define a timestamp along with the date, or you need to filter by the day after for less than equal to or the day before for greater than equal

How to properly insert date into MYSQL database using php?

I want to store and display date in this format "1.1.1990", (d.m.Y)
I've got a date column in my database with these settings
TYPE: DATE
Browser display transformation: Date Format(text/plain: Dateformat)
Browser display transformation options: 0,'%j.%n.%Y','local'
My query goes as :
INSERT INTO table VALUES(STR_TO_DATE('1.1.1990', '%e.%c.%Y'))
So far the query is accepted but a blank field is displayed in database
Only other thing i could get is without using STR_TO_DATE to add a default date as 1.1.1970
You have to set the column in date type
You can ensure the date is wellformed: select STR_TO_DATE('1.1.1990', '%e.%c.%Y');

error in save date in database with php

want to be able to store the current date from PHP in to a mysql table.
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."','".$date."','".$yes."')";
Date's column type in database is DATE.
but date save in database like this: 0000-00-00
help me.
try this
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."',".$date.",'".$yes."')";
or
if you want to inset today's date use
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
I need to store Date field in database for example VARCHAR(10) becouse, DATE type in mysql server don't support persian (farsi) date.
If you are getting the current date, just use one of the built-in MySQL functions for that:
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
You won't ever have to worry about formatting or any sort of funny inputs and you can still do all sorts of addition, subtraction and the like using one of the many other functions.
Edit: I have just looked up some information on the date system and you will run into problems trying to squeeze that data into a date field. The first six months have 31 days, the next 5 have 30 days, then the last has 29 - unless it is a leap year in which case it has 30. The date field simply won't LET you insert these values into a date field.
You might need to convert your data to a timestamp and write a function to encode/decode it into the format you need.

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Categories