jQuery datepicker not showing time but the date works - php

my jquery datepicker shows date correctly but the time isn't working. the test result produces the following: 1372046400 = 2013-06-24 04:06:00
as you can see the time isnt same as the systemtime, it should be 8:19pm.
<script type="text/javascript">
$(document).ready(function(){
$("#date").datepicker({
showButtonPanel: true,
minDate: '0M',
maxDate: '+90D',
dateFormat: "d-MM-yy",
onSelect : function(dateText, inst)
{ var epoch = $.datepicker.formatDate('#', $(this).datepicker('getDate')) / 1000;
$('#hidden1').val(epoch);
}
});
});
</script>
php script that displays the output
$dateTime = $_POST['hidden1'];
$date = new DateTime('#'.$dateTime);
echo $saveDate = $date->format('U = Y-m-d h:m:s');

http://php.net/manual/en/function.date.php
it has to be
$date->format('U = "Y-m-d H:i:s');
m is for month

set your time zone (http://php.net/manual/en/function.date-default-timezone-set.php) and then try to use the answer that Sanath has suggested.
Example:-
date_default_timezone_set('America/Los_Angeles');

Related

JQuery UI Datepicker SetDate

I use the popular JQuery UI Datepicker to add/modify events on my web site. I am having a problem with getting the date to show up right using setDate. The incorrect date seems to always show up no matter what I do. Here are some various things I have tried:
The date shows up as 01/17/1970 when it should be 10/15/2015, this is how I need it to work, with the php:
$(function() {
var date = new Date(<?php echo strtotime($date); ?>);
$("#date").datepicker();
$("#date").datepicker("setDate", date);
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
The date has inconsistent values when done like this, the date will always be wrong no matter what date you set, but there is no pattern.
$(function() {
var date = new Date(2015, 10, 15);
$("#date").datepicker();
$("#date").datepicker("setDate", date);
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
The Javascript Date object's constructor takes milliseconds when given only one argument.
The PHP function strtotime() returns the Unix Timestamp, the number of seconds since 1970-01-01.
If you multiply the timestamp by 1,000, you'll get a suitable value to pass to the Date constructor.
var date = new Date(<?php echo (strtotime($date) * 1000); ?>);
I had also tried it like this previously, but forgot to put quotation marks around the php. Now it works:
<script>
$(function() {
$("#date").datepicker();
$("#date").datepicker("setDate", "<?php echo $date ?>");
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
</script>

Use a timedate defined in a php variable as the starting point for a datetimepicker range

Instead of the servers system clock being used as the starting point for the range minDate 0 and maxdate 7 I'd like to use the time I have stored in the php variable $whentime in the format YYYY-MM-DD HH:MM.
<script>
$(function() {
$( "#datetimepicker" ).datetimepicker({
hourGrid: 4,
minuteGrid: 10,
dateFormat: 'mm-dd-yy',
timeFormat: 'hh:mm tt',
numberOfMonths: 2,
minDate: 0,
maxDate: 7
});
});
</script>
If you want to explicitly set the time based on that PHP variable, you could do this:
<?php $whentime = date('Y-m-d H:i'); ?>
<script type="text/javascript">
$('#datetimepicker').datetimepicker({
value:'<?php echo $whentime; ?>',
format:'Y-m-d H:i',
});
</script>

changing date format same as MySQL format in jQuery datepicker [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 8 years ago.
I am working with jQuery date picker, and the problem is the format of the date, i want the format same as the MySQL date format as i do not get the correct value of the date in MySQL table, i am pasting the code below.
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#pdate').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate)
});
});
</script>
Form
<input type="text" name="date" title="" placeholder="mm / dd / yyyy" class="date" id="pdate" required="required"/>
Currently the date picker's date format is 05/16/2014 and MySQL's date format is MM-DD-YYYY
do like this
$("#pdate").datepicker({showOn: 'focus',changeMonth: true,
changeYear: true,dateFormat: 'mm-dd-yy',
minDate: new Date(currentYear, currentMonth, currentDate)
});
You need to add dateFormat attribute to your datepicker() function.
for more info check here
add dateFormat to your datepicker
$('#pdate').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: 'mm-dd-yy'
});

jquery date picker doesn't appear

jQuery code:
<script src="js/jquery-1.10.1.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#book').validate({
rules : {
name : 'required',
sdate : {
required : true,
date : true
},
seats :{
required : true,
number : true,
range:[1,9]
},
},// end rules
}); // end validate()
$('#sdate').datepicker({
minDate : new Date("<?php echo $start;?>"),
maxDate : new Date("<?php echo $end?;>")
});
});
</script>`
Value for date is retrieved from the MySQL database and stored in the variable after converting it to date format. Its passed to the date picker - minDate and maxDate. If I manually pass the date to the minDate and maxDate, the datepicker appears, in the above case even the validation doesn't work nor does the datepicker appear.
PHP code:
$st=$value->getStart();
$start = date("Y-m-d h:i:s", strtotime($st));
$ed=$value->getEnd();
$end = date("Y-m-d h:i:s", strtotime($ed));
HTML code:
Show Date <input name="sdate" type="text" id="sdate" />
Any help on this issue is appreciated. Thanks in advance.
Time in format Y-m-d h:i:s will not with javascript's Date object. Try to use for example this format: D M d Y H:i:s O
$st=$value->getStart();
$start = date("D M d Y H:i:s O", strtotime($st));
$ed=$value->getEnd();
$end = date("D M d Y H:i:s O", strtotime($ed));

UIDatePicker - minDate & maxDate from php Var

I have some php which queries a database to get some dates:
<?php
//connect to db etc
$sql="SELECT date FROM entries ORDER BY date ASC";
$result = mysql_query($sql);
if($result){
$entryDates = array();
while ($row=mysql_fetch_array($result)) {
$entryDates[] = $row['date'];
}
$minDate = explode("-", $entryDates[0]);
$minDate[1] -= 1;
$maxDate = explode("-", end($entryDates));
$maxDate[1] -= 1;
echo($maxDate[1]);
}
?>
I then try and assign the min and max dates to a uidatepicker using jquery.
Originally I did this and it worked:
var theDate = new Date();
$( "#datepicker" ).datepicker(
{
dateFormat: 'dd-mm-yy',
maxDate: new Date(theDate.getFullYear(), theDate.getMonth(), theDate.getDate())
}
)
However trying to implement the same using the php vars doesn't work:
$( "#datepicker" ).datepicker(
{
dateFormat: 'dd-mm-yy',
maxDate: new Date(<?php echo($maxDate[2]);?>,<?php echo($maxDate[1]);?>,<?php echo($maxDate[0]); ?>)
minDate: new Date(<?php echo($minDate[2]);?>,<?php echo($minDate[1]);?>,<?php echo($minDate[0]); ?>)
}
);
I am trying to use the following date constructor:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
$minDate and $maxDate are arrays that look something like
[yyyy|mm|dd]
When echoing them they contain the correct values..so the issue lies within jQuery...probably the way I am trying to create a Date...but what exactly is wrong with my syntax? I cannot see what I am doing wrong...
Your array data is probably being placed into the javascript Date() constructor in the wrong order. Assuming your date strings are in the format YYYY-MM-DD and you print_r() your $maxdate array you should see something like this:
Array
(
[0] => 2011
[1] => 0
[2] => 01
)
Notice that this array's indices are in the order 0=>year, 1=>month, 2=>day. So you need to modify your Date() constructors to align those values with the constructors input parameters like this:
<script type="text/javascript">
$( "#datepicker" ).datepicker({
dateFormat: 'dd-mm-yy',
maxDate: new Date(<?php echo($maxDate[0]);?>,<?php echo($maxDate[1]);?>,<?php echo($maxDate[2]); ?>),
minDate: new Date(<?php echo($minDate[0]);?>,<?php echo($minDate[1]);?>,<?php echo($minDate[2]); ?>)
});
</script>

Categories