how to use variable in mysql date format - php

i'm passing variable it show result but no record selected
What am I doing wrong?
$starting_date=$_REQUEST['first'];
$ending_date=$_REQUEST['last'];
$result=mysql_query("SELECT * FROM class
WHERE `date` between DATE_FORMAT( $starting_date,'%Y-%m-%d')
and DATE_FORMAT($ending_date,'%Y-%m-%d')");

It seems that you have datetime datatype in your mysql table. You should try Something like this:
$result=mysql_query("SELECT * FROM class
WHERE DATE(`date`) between '$starting_date' and '$ending_date'");
if your starting date and ending date is in this format: YYYY-MM-DD
EDIT::JQuery Datepicker:
$(function() {
$( "#starting_date" ).datepicker({
dateFormat : 'yy-mm-dd'
});
$( "#ending_date" ).datepicker({
dateFormat : 'yy-mm-dd'
});
});

thank you everyone to help me
this funcation change date format
$(function() {
$( "#starting_date" ).datepicker({
dateFormat : 'yy-mm-dd'
});
$( "#ending_date" ).datepicker({
dateFormat : 'yy-mm-dd'
});
});
and this query help me :)
$result=mysql_query("SELECT * FROM class
WHERE DATE(date) between '$starting_date' and '$ending_date'");

Related

How to get month in number from date in "JAN-2014" format?

Actually, i am using jquery datepicker and getting month in "MONTH-YEAR" format?
how i can get month number form this format in PHP or JAVASCRIPT?
js code:
$("#date1_live").datepicker({
changeMonth: true,
changeYear: true,
showAnim:'slideDown',
dateFormat:'M yy'
});
html text field:
<input type="text" name="reportMonth" id="date1_live" required Placeholder="Select Start Date" >
using php try like this
$month = 'JAN-2014';
echo date('m', strtotime($month));
Please take a look at the following documentation.
jQuery UI Datepicker
The format for parsed and displayed dates. For a full list of the
possible formats see the formatDate function.
Code examples:
Initialize the datepicker with the dateFormat option specified:
$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });
try this,
alert($("#datepicker").datepicker({ dateFormat: "mm-yy" }).val());
or Demo
{
changeMonth: true,
changeYear: true,
showAnim:'slideDown',
dateFormat:"mm-yy"
}

Date Field in Firefox no working with MYSQL

I am having trouble with this. So, I created a from that allows you select a date range. Then from there, the form populates with records from the database based on the date range. Pretty easy.
However, the date field didn't work in firefox, so I tried switching it to jquery. and how it doesn't work. I am assuming it's something with the format. So here is my jquery code:
<script>
$(function() {
$( "#from" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3,
onClose: function( selectedDate ) {
$( "#to" ).datepicker( "option", "minDate", selectedDate );
}
});
$( "#to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3,
onClose: function( selectedDate ) {
$( "#from" ).datepicker( "option", "maxDate", selectedDate );
}
});
});
</script>
and here is the mysql statement:
$sql = "SELECT * FROM v88374 WHERE date >= DATE_FORMAT('" . $from . "', '%Y/%m/%d') AND date <= DATE_FORMAT('" . $to . "', '%Y/%m/%d')";
Any help is greatly appreciated. I need it to work in all browsers ahd the reason I switched jquery was because the basic date field wouldn't work in firefox....so if someone has a better fix for this then I am definitely open to it.
You should not be treating dates as strings in your SQL query. Use the appropriate data type in your database (probably DATE). You should then parameterize your query rather than appending the values into the string.
The formatting concern should be delegated to your PHP code. Parse the input into a datetime class, and pass that as a parameter. (Use the strptime function to do the parsing.)

jquery datepicker change date format to yyyy-mm-dd php [duplicate]

This question already has answers here:
How to change date format (MM/DD/YY) to (YYYY-MM-DD) in date picker
(18 answers)
Closed 9 years ago.
Hi I am trying to format the jquery datepicker so that it displays as yyyy-mm-dd
I have both a start_date and end_date..
I have tried changing the script in the head to:
<script>
$(function() {
$( "#datepicker" ).datepicker();
$( "#datepicker" ).datepicker({dateFormat: 'yy-mm-dd'});
});
</script>
however it still appears when I am trying to put it in a database as 00-00-000
I would really appreciate any help!
Ryan
$(function(){
$("#to").datepicker({ dateFormat: 'yy-mm-dd' });
$("#from").datepicker({ dateFormat: 'yy-mm-dd' }).bind("change",function(){
var minValue = $(this).val();
minValue = $.datepicker.parseDate("yy-mm-dd", minValue);
minValue.setDate(minValue.getDate()+1);
$("#to").datepicker( "option", "minDate", minValue );
})
});
http://jsfiddle.net/gaby/WArtA/

jQuery UI datepicker: Displaying 1969 although I use strtotime

I have PHP+JQUERY room reservation application. I'm using the datepicker widget for picking date and I encounter a problem. I try to convert the selected date (in dd/mm/yyyy format) to YYYY-mm-dd format in order to INSERT it to my database. When I pick the first dates, it converts well but when I pick other dates, I see the date 1969-12-31. Here is my JQUERY code:
$(function() {
$( "#datepicker" ).datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
minDate: 0,
maxDate: "+3W",
dateFormat: "dd/mm/yy",
beforeShowDay: function (date) {
var day = date.getDay();
return [(day == 0 || day == 1 || day == 2 || day == 3 || day == 4), ''];
},
onSelect: function(dateText) {
$("#registration").load("room.php #registration", {selectedDate: dateText}, function() {
$( "input:submit, a, button", ".registration" ).button();
$( "a", ".registration" ).click(function() { return false; });
});
}
});
});
And then i echo the result for testing:
<?php if(isset($_POST['selectedDate']))
{
$selectedDate=$_POST['selectedDate'];
echo date('Y-m-d',strtotime((string)$selectedDate));
}
?>
Here is an image in my app: http://oi43.tinypic.com/29tv2c.jpg
1:
Eventually i by passed the problem. instead of converting it in PHP, i converted it in JS:
onSelect: function(dateText) {
//Converting the date format by spliting the date.
var dt= dateText;
var arrDt = dt.split('/');
var newDt = arrDt[2] + "-" + arrDt[1] + "-" + arrDt[0];
//Loading the rooms div acoording to the sent selected date in JSON format
$("#registration").load("room.php #registration", {selectedDate: newDt}, function() {
$( "input:submit, a, button", ".registration" ).button();
$( "a", ".registration" ).click(function() { return false; });
});
}
But i still don't know what caused the date() function conversion problem!
What's type of selected date that you store in MySQL? With DATE data type you just have to pass 'YYYY-MM-DD format to INSERT statement.
See this question for more details
And also this http://dev.mysql.com/doc/refman/5.5/en/datetime.html

jquery datepicker manipulation

Can someone please tell me how I can make the datepicker pick a date that is after a certain date I choose.
For example I want the date picker to pick dates after 03/08/2012.
Here is my simple datepicker:
<script type="text/javascript">
$(function(){
$("#date").datepicker({ dateFormat: 'm/d/yy', minDate: 0});
});
</script>
What if the date was stored in a php variable. for eg. $var='03/08/2012'. Is splitting it one by one only way to do it?
I am trying to get the date from a dynamic php variable. For eg:
<script type="text/javascript">
$(function() {
$( "#date" ).datepicker({ minDate: new Date(<?php echo $split_date[2];?>, <?php echo ($split_date[0] - 1);?>, <?php echo $split_date[1]; ?>)});
});
</script>
<?php
$date='03/08/2011';
$split_date=explode("/",$date);
echo $split_date[0];
echo $split_date[1];
echo $split_date[2];
?>
but nothing i tried so far works!
According to jQuery's doc on the Datepicker, you should do
<script>
$(function() {
$( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
});
</script>
Restrict the range of selectable dates with the minDate and maxDate options. Set the beginning and end dates as actual dates (new Date(2009, 1 - 1, 26)), as a numeric offset from today (-20), or as a string of periods and units ('+1M +10D'). For the last, use 'D' for days, 'W' for weeks, 'M' for months, or 'Y' for years.
If you want it to start from 03/08/2012, you should write
<script>
$(function() {
$( "#datepicker" ).datepicker({ minDate: new Date(2012, 3 - 1, 8)});
});
</script>
You can also refer to this answer:
changing minDate option in JQuery DatePicker not working
Try:
<script type="text/javascript">
$(function(){
$("#date").datepicker({
'dateFormat': 'm/d/yy',
'minDate': new Date(<?php echo date('Y, m, d'); ?>)
});
});
</script>

Categories