jquery date picker doesn't appear - php

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));

Related

strtotime with datetimepicker

I'm using the datetimepicker jquery plugin and have the following code to display it:
var dateToday = new Date();
$('#post_date_picker').datetimepicker({
minDate: dateToday,
timeFormat: 'hh:mm p z',
timezoneList: [
{ value: -300, label: 'Eastern'},
{ value: -360, label: 'Central' },
{ value: -420, label: 'Mountain' },
{ value: -480, label: 'Pacific' }
]
});
Since I'm displaying it in 12 hour format instead of 24 and using the min date if the server time is already PM I can select the time and it outputs it like 05/27/2015 08:50 p -0500
I then do the following code to get the date ready to be inserted into the database:
$date_time = strtotime($_POST['post_date']);
$post_date = date('Y-m-d H:i:s', $date_time);
The problem is strtotime isn't recongining the p for PM and is inserting the date basically 12 hours previously. How can I do this?
Set timeFormat: 'hh:mm tt z'.
And in php use date_create & date_format ( $date_time,'Y-m-d h:i:sA')
See the difference between( strtotime / date_create ).
// method using date_create
//$date=$_POST['post_date'];
$date='05/27/2015 08:50 p -0500';
$date_time=date_create($date);
$post_date=date_format($date_time,'Y-m-d H:i:s');
echo $post_date."<br>";//outputs 2015-05-27 08:50:00
//method using strtotime
$date_time = strtotime($date);
$post_date = date('Y-m-d H:i:s', $date_time);
echo $post_date;//outputs 2015-05-27 13:50:00
strtotime can take many string parameters, may be when you pass the date as '05/27/2015 08:50 p -0500' some conflict is occurring resulting in wrong interpretation of time.

jQuery datepicker not showing time but the date works

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');

JQuery Full Calendar date format

Im using JQuery Full Calendar Plugin. Code:
$('#mycalendar').fullCalendar({
** options **
events: function(start, end, callback) {
$.ajax({
url: '/myloader/',
dataType: 'json',
data: {
// our hypothetical feed requires UNIX timestamps
start: Math.round(start.getTime() / 1000),
end: Math.round(end.getTime() / 1000)
}
*** more stuff
});
now on myloader php side when i try to get start and end dates here is what i get:
var_dump(date('m/d/Y H:i:s', $_GET['start']), date('m/d/Y H:i:s', $_GET['end']));
this returns:
string(19) "01/27/2013 06:00:00"
string(19) "03/10/2013 06:00:00"
why is it 6:00:00 ? i want it to be 00:00:00 for start and 23:59:59 for end
I know i can hack through it using PHP but is there a reason why full calendar returns such date?
If i use PHP i can get desired results using:
$start = strtotime(date('m/d/Y', $start) . ' 00:00:00');
$end = strtotime(date('m/d/Y', $end) . ' 23:59:59');
but i dont want to do it on PHP side is there a way full calendar to give correct time?
If its a timezone issue how can it be fixed?
thanks
How about this:
var_dump(
date('m/d/Y H:i:s', strtotime(date("m/d/Y",(int)$_GET['start'])),
date('m/d/Y H:i:s', strtotime(date("m/d/Y",(int)$_GET['end']))
);

How to convert format date from PHP to Javascript

I have a php file which I will later change into the form of an HTML file, which be current constraints are:
- Date format using Javascript.
Her script like the following snippet:
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<?php
/* Set start and end dates here */
$startDate = strtotime("15 August 2012 12:00:00");
$endDate = strtotime("15 November 2012 12:00:00");
/* /Set start and end dates here */
?>
<script type="text/javascript">
$(document).ready(function(){
JBCountDown({
secondsColor : "#ffdc50",
minutesColor : "#9cdb7d",
hoursColor : "#378cff",
daysColor : "#ff6565",
startDate : "<?php echo $startDate; ?>",
endDate : "<?php echo $endDate; ?>",
now : "<?php echo strtotime('now'); ?>",
seconds : "<?php echo date("s"); ?>"
});
});
</script>
please help me to overcome it.
I want to run a php script in javascript that is being subordinated.
example:
<script type="text/javascript">
$(document).ready(function(){
JBCountDown({
secondsColor : "#ffdc50",
minutesColor : "#9cdb7d",
hoursColor : "#378cff",
daysColor : "#ff6565",
startDate : startDate, //format time in JS
endDate : endDate, //format time in JS
now : strtotime('now'), //format time in JS
seconds : date("s")
});
});
</script>
Please help,
Regards
this can all be done in javascript.
<script type="text/javascript">
$(document).ready(function(){
//http://www.convert-unix-time.com/
var unix = Math.round(+new Date()/1000); //unix timestamp for todays date
//alert(unix);
JBCountDown({
secondsColor : "#ffdc50",
secondsGlow : "none",
minutesColor : "#9cdb7d",
minutesGlow : "none",
hoursColor : "#378cff",
hoursGlow : "none",
daysColor : "#ff6565",
daysGlow : "none",
startDate : "1362096000 ", //unix timestamp for ' Friday 1st March 2013 12:00:00 AM'
endDate : "1373328000", //unix timestamp for 'Tuesday 9th July 2013 01:00:00 AM'
now : unix,
seconds : unix % 60 //unix timestamp for seconds in realtime
});
});
</script>
Use strtotime() and date():
$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).
<?php
/* Set start and end dates here */
$startDate = new DateTime("15 August 2012 12:00:00");
$endDate = new DateTime("15 November 2012 12:00:00");
/* /Set start and end dates here */
print $startDate->format('FORMAT');
?>
See all date time formats: http://www.php.net/manual/en/datetime.formats.php

How to correctly pass JSON from php to Javascript with Dates?

I am tinkering with the Jquery Weekly Calendar Plugin (Which you can look at the full demo here: skedyul.herokuapp.com)
Now, I'm trying to put some events dynamically using PHP but I'm fairly new to the concept of JSON.
This is my example php array to be encoded to JSON:
<?php
$events = array(
array(
"id" => 1,
"start" => date("D M j Y H:i:s "),
"end" => date("D M j Y H:i:s ",strtotime("+30 minutes")),
"title" => "College Algebra"
),
array(
"id" => 2,
"start" => date("D M j Y H:i:s ",strtotime("+30 minutes +1 day")),
"end" => date("D M j Y H:i:s ",strtotime("+45 minutes +1 day")),
"title" => "Calculus"
)
);
?>
And here is the working Javascript code with some sample data:
<script type="text/javascript">
function getEventData() {
var year = new Date().getFullYear();
var month = new Date().getMonth();
var day = new Date().getDate();
console.log(new Date(year, month, day, 12));
return {
events : [
{
"id":1,
"start": new Date(year, month, day, 12),
"end": new Date(year, month, day, 13, 30),
"title":"College Algebra",
},
{
"id":2,
"start": new Date(year, month, day, 14),
"end": new Date(year, month, day, 14, 45),
"title":"Statistics and Probability"
}
]
};
}
And here, I changed the above Javascript into this (putting the json encoded php associative array)
<script type="text/javascript">
function getEventData() {
var year = new Date().getFullYear();
var month = new Date().getMonth();
var day = new Date().getDate();
console.log(new Date(year, month, day, 12));
return {
events : <?php echo json_encode($events)?>
};
}
</script>
It's seems to work an doesn't spews out errors but the problem is about the Date.
It's not showing properly at the calendar..
How do I correctly pass JSON from PHP with dates?
Thank you :)
Please consider: date.js from http://code.google.com/p/datejs
You can use Date.parse() or Date.parseExact()
In your case, you use Date.parseExact(dateString, "d MMM d yyyy HH:mm:ss")
In your page, include the date.js.
<script src="http://yoursite/path/to/date.js"></script>
Then try the following...
var dateobj = Date.parseExact(dateString, "d MMM d yyyy HH:mm:ss")
alert(dateobj.toString());
Please read http://code.google.com/p/datejs/wiki/FormatSpecifiers for date formatting

Categories