I want to insert date in format m-d into database. I used this code
$date1=date("m-d",mktime(0,0,0,$month1 ,$day1,1));
The date is inserted as 0000-00-00 in MySQL, and I don't know why.
I want to do that to compare that date stored in MySQL with current month and day. How can this be done?
mysql date must be of the form Y-m-d . Building a date with m-d form brings an invalid date.
If you want to compare only month or day, you can use mysql MONTH and DAYOFMONTH functions. For example:
$query = "SELECT * FROM table WHERE MONTH(datecol) = {$month1} AND DAYOFMONTH(datecol) = {$day1}"
$date1=date("Y-m-d",mktime(0,0,0,$month1 ,$day1,1));
Related
I am using jQuery DatePicker to select from date and to date. when I select a date i covert it to timestamp using strtotime function, I have some dates stored in my database with column type timestamp.
when I convert the selected date from date picker using strtotime
$from_date = strtotime($form['from-date']);
it gives me something like this
1338508800
while the date stored in my database table looks like this
2012-06-09 02:24:25
I want to query the records on the basis of the selected from and to date. but the query is not getting my dates correctly.
How can I change the selected date so that it give me results in between the two selected dates?
You want date('Y-m-d H:i:s',strtotime($form['from-date']));
I have a UNIX timestamp in a table like 1321521158 and want to get the details from a table for the particular date, like:
$mydate="11/11/2011";
SELECT notes FROM table WHERE `addeddate`=$mydate
How do I get it?
you can use the mysql date function like:
WHERE date(addedate) = date(mydate)
or you can calculate the starting and ending timestamp for the given day you want to know and then do
WHERE addeddate >= mystarttimestamp AND addeddate <= myendtimestamp
try
WHERE $mydate = FROM_UNIXTIME('dateColumn')'%Y/%D/%M');
MySQL Date Time Functions
Like many people, I am totally confused by the many date functions in PHP and MySQL. What I need is a to be able to store a date in MySQL and be able to view it on the screen in a human readable format, search on it by month, year, or combination of both using a standard web form, or sort it on months or years.
Example search would be all the records for febuary for the past 5 years.
I have a javascript calendar that inputs the month in the form as 02-12-2011.
What is the best format to use for this. What should the field be in MySQL.
Thanks
Please make use of the DateTime object.
Store the dates in mysql as a DATE format.
When writing the data
$date = new DateTime($_POST['date']);
or
$date = DateTime::createFromFormat('d-m-Y', $_POST['date']);
$query = sprintf("INSERT INTO `data` SET `date` = '%s'", $date->format('Y-m-d'))
When reading the data out create a DateTime object.
$date = new DateTime($row['date']);
Then you can print it in whatever format you want, e.g. You javascript's format:
echo $date->format('d-m-Y');
See
http://www.php.net/manual/en/class.datetime.php
and for date formats:
http://www.php.net/manual/en/function.date.php
As far as searches go, you can use mysql Date functions on the fields.
For all records in February for the last 5 years.
SELECT * FROM `data` WHERE MONTH(`date`) = 2 AND YEAR(`date`) >= YEAR(NOW()) - 5
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
The column type in MySQL should be date.
it's a date, so store it as a DATE column. You can either use UNIX_TIMESTAMP() in your SQL query or strtotime in PHP, to convert this back to a value that can be passed in to the php date() function, to output whatever format date you'd like.
I am trying to select all records in a table which have a date between the current date and 1 month ahead.
The date is stored like this DD-MM-YYYY
And the query I have tried:
SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)
But this does not return the correct results.
Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.
i want to compare database field of date that has YYYY-MM-DD
format and i want to compare from MM-DD
actually i want to fetch records those have birthdays between month /selected week durations
and birthdate is stores as YYYY-MM-DD format in db table
how can i achieve this kindly help.
Look at this:
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
you can use CURDATE(), DATE_ADD() and DATE_SUB() to make date interval
Within your sql you could try something like the following:
select * from people where birthdate>='10/01/2010' AND birthdate<='30/01/2010';
if you want to get more specific with months and days then you can utilise the month(birthdate) and day(birthdate) functions in mysql;
select * from people where month(birthdate)=10;
would return all your people where the month for birthdate is October.