I have one problem, which I can not resolve and have no idea where is mistake
One I create a tabel called posts and in codeigniter create a form Posts, after createing all post I want to display posts in index.php and I have table called
Date of departure, and date of return. One i create post date looking good but when I display in my index.php it's look like 0000-00-00 00:00:00
In begginig data type was Date, now i change to be DateTime but nothing happend, same problem
Maybe I make mistake in my JS script
Any comment ?
My script
<script>
$( function() {
$( "#datepicker" ).datepicker();
$( "#datepicker1" ).datepicker();
} );
</script>
When your field in MySQL is Date-time it aspects proper format before saving in database.
So convert string to date time format in php using.
date("Y-m-d H:i:s", strtotime($string));
Or you can do the same in Datepicker
$(function() {
$('#datepicker').datepicker({
dateFormat: 'yy-dd-mm',
onSelect: function(datetext){
var d = new Date(); // for now
var h = d.getHours();
h = (h < 10) ? ("0" + h) : h ;
var m = d.getMinutes();
m = (m < 10) ? ("0" + m) : m ;
var s = d.getSeconds();
s = (s < 10) ? ("0" + s) : s ;
datetext = datetext + " " + h + ":" + m + ":" + s;
$('#datepicker').val(datetext);
},
});
});
Fiddle for datepicker
Note if you want date only in MySQL field the omit hour min sec part of conversion
Related
im a javascript / jQuery n00b so bear with me
I have a table in the database that contains multiple rows with startdate & enddate columns
I have on the page a datepicker jquery element
What I need it to do is on the page load check the database table and all the dates from startdate to enddate to be disabled
I have the below which would do individual dates however I dont know how to get it to loop and disable multiple entries ( ranges - from date to date )
For example 24-4-2017 to 26-4-2017 disabled also 30-4-2017 to 17-5-2017 disabled
so multiple ranges need to be disabled
Code I have so far below, please help guys Ive been racking my brains over this for 2 days now and have severe code block
var disabledDays = ["22-04-2017"]; // M-DD-YYYY Format
/* utility functions */
function nationalDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
//console.log('Checking (raw): ' + m + '-' + d + '-' + y);
for (i = 0; i < disabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
//console.log('bad: ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
return [false];
}
}
//console.log('good: ' + (m+1) + '-' + d + '-' + y);
return [true];
}
/*
Above 2 are probably redundant because BaT are open Bank Holidays
and also open weekends so ..
*/
/*
Create DatePicker
Change MaxDate below to show more months
*/
jQuery(document).ready(function() {
jQuery('#date').datepicker({
minDate: new Date(2017, 0, 1),
maxDate: new Date(2017, 6, 31),
dateFormat: 'DD, MM, d, yy',
constrainInput: true,
beforeShowDay: nationaldays
});
});
Just compare the day that is currently rendering with disabled dates range.
$(document).ready(function() {
var startDisabledDates = new Date(2017, 03, 10),
endDisabledDates = new Date(2017, 03, 20);
$("#date").datepicker({
beforeShowDay: function(day) {
var isSelectable = day < startDisabledDates || day > endDisabledDates;
return [isSelectable];
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="date">
Hiii..
My concept is an agent wants add date from backend which is from(6/07/2015) to until(20/07/2015) these dates will store in database. if agent want disable 2 days(ex. 12/07/15 and 14/07/2015) then how to show these dates disable in datepicker on fron end. Because these two day agent haven't product. That's why agent wants these two day disable and remaining day 6 to 20 should be enable.
Please any body have any idea please help me.
Thanks!!!
please refer this link. this is working example with jquery datepicker.
Please see below code.
var unavailableDates = ["9-3-2012", "14-3-2012", "15-3-2012"];
function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == -1) {
return [true, ""];
} else {
return [false, "", "Unavailable"];
}
}
$(function() {
$("#iDate").datepicker({
defaultDate: new Date("3-1-2012"),
dateFormat: 'dd MM yy',
beforeShowDay: unavailable
});
});
Bootstrap datepicker:
In the bootstrap datepicker you can use the method:
datesDisabled
String, Array. Default: []
Array of date strings or a single date string formatted in the given date format
for example:
$('#sandbox-container input').datepicker({
datesDisabled: ['07/06/2015', '07/21/2015']
});
JQUERY datepicker UI
In this use the beforeShowDay method
var unavailableDates = ["9-5-2011","14-5-2011","15-5-2011"];
function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) < 0) {
return [true,"","Book Now"];
} else {
return [false,"","Booked Out"];
}
}
$('#iDate').datepicker({ beforeShowDay: unavailable });
refernce for JQUERY datepicker UI
I am developing application for hotel booking..
I have used 2 datepickers for selecting check-in & check-out date..
But now i want my first date-picker's current value to be shown on that textbox by default & another textbox of second datepicker to be filled by date of 2 days after...
Just like these datepickers....
http://www.yatra.com/hotels.html
can anyone suggest me,how to do this??
I have used same datepickers as shown in link..
thanks in advance.
You can set a date like this (if the datepicker is initialized)
$('#your-selector')
.datepicker('setDate',new Date());
of a uninitialized datepicker use:
$('#your-selector')
.datepicker().datepicker('setDate',new Date());
to create a date 2 days before... make this:
var today = new Date();
var twoDaysBefore = new Date();
twoDaysBefore.setDate(today.getDate()-2);
and set the date
$('#your-selector')
.datepicker('setDate', twoDaysBefore);
you can create a function if you need this algorithm more times:
var getDate = function(daysDelta) {
var d = new Date();
(d.setDate(d.getDate()+daysDelta));
return d;
}
console.log(getDate(-3))
without jQuery (thanks), format your date object like this:
var d = getDate(-2);
var yyyy = d.getFullYear().toString();
var mm = (d.getMonth()+1).toString();
var dd = d.getDate().toString();
var dateAsString = yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+1);
document.getElementById('id').setAttribute('value', dateAsString);
Todays Date
$("#date_pick").datepicker('setDate', new Date());
Date of 2 days after
var nextDate = new Date(new Date().getTime() + 48 * 60 * 60 * 1000);
$("#date_pick2").datepicker('setDate', nextDate);
try this code
$('first').datepicker({
onSelect: function(){
var start = $('first').val();
var date = new Date(start);
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var edate= new Date(y, m, d+2);
$('second').val(edate);
}
});
If datepicker1 is your datepickers id and datepicker2 is the second one then
$("#datepicker1").val(new date());
var date2 = $('datepicker1').datepicker('getDate', '+2d');
date2.setDate(date2.getDate()+1);
$("#datepicker2").val(date2);
I am trying to insert local time into database using javascript. Can any one help me?
could you please have a look on the file named member.class.php and please let me know where I have to put the javascript code... https://www.dropbox.com/sh/lfef67brewx7rub/wYRP72bDh7
Its very simple man...try with this
var milliseconds = new Date().getTime();
it will give you time in milli seconds or you can use like
new Date().getTime();
if you want it in unix(linux) timestamp then use like this
var unix = Math.round(+new Date()/1000); it will also give you in seconds
var currentdate = new Date();
var datetime = "Date n Tym : " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
10 ways to format time and date using JavaScript
See this answer!
In Javascript:
var d = new Date();
var date_with_time = d.getDate()+"-"
+d.getMonth()+1+"-"
+d.getFullYear()+" "
+d.getHours()+" "
+d.getMinutes()+" "
+d.getSeconds();
In PHP, the best way will be that you should create your time column DATETIME type and set default value CURRENT_TIMESTAMP. No need to manually insert it through Javascript.
DB like time stamp in js:
var timestamp = "" + d.getFullYear()
+ ("0" + (d.getMonth()+1)).slice(-2)
+ ("0" + d.getDate()).slice(-2) + "-"
+ ("0" + d.getHours()).slice(-2)
+ ("0" + d.getMinutes()).slice(-2)
+ ("0" + d.getSeconds()).slice(-2);
after this, for example, timestamp="20160408-134304"
I have created a timer to be used as a reference on a alert when the timer reaches exactly 5:00 pm/ 17:00. Do you have any ideas how could I do that? Do I check the time once in a while?
Here's how I create my timer.
<script language="JavaScript">
var tick;
function stop() {
clearTimeout(tick);
}
function usnotime() {
var datetoday = document.getElementById("datenow")
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
datetoday.val("" + month + "/" + day + "/" + year + "");
var txt = document.getElementById("timeticker")
var ut = new Date();
var h, m, s;
var time = " ";
h = ut.getHours();
m = ut.getMinutes();
s = ut.getSeconds();
if (s <= 9) s = "0" + s;
if (m <= 9) m = "0" + m;
if (h <= 9) h = "0" + h;
time += h + ":" + m + ":" + s;
txt.innerHTML = time;
tick = setTimeout("usnotime()", 1000);
}
//--> document.rclock.rtime.value=time; tick=setTimeout("usnotime()",1000); } //-->
</script>
<tr>
<td class="tdlabel">Current Time:</td>
<td>
<div id="timeticker" name="rtime" size="22"></div>
</td>
</tr>
And it seems that my timer is not displaying. Any ideas??
You have a mistake, you mixed javascript and jquery.
datetoday.val("" + month + "/" + day + "/" + year + "");
Should be (assuming datetoday is a textfield. If div use innerHTML instead of value)
datetoday.value="" + month + "/" + day + "/" + year + "";
and add just before the closing script tag
window.onload=function(){
usnotime();
}
Not sure if this is the exact answer; it might help posting a bit more code.
I don't see how you're starting the method. You might need to add a button-execute / page-load call of usnotime().
Do you have real objects for all of those variables? (I.e. timeticker, datenow).
Check your JavaScript error log in the browser. It's a bit hard without more code for me to see what's wrong, so either include more, or use the debugger.
Using the setTimeout() u can do , Refer this it may be useful for you
This may help you....
<html>
<head>
<title>(Type a title for your page here)</title>
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var strcount
var x
var x1
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds= currentTime.getSeconds()
x=month + "/" + day + "/" + year
if (minutes < 10){
minutes = "0" + minutes
}
x=x+" "+hours + ":" + minutes +":"+seconds+ " "
x1=hours + ":" + minutes + ":"+seconds+" "
if(hours > 11){
x=x+"PM"
x1=x1+"PM"
if(x1=="5:30:0 PM"){alert(x1);}
} else {
x=x+"AM"
x1=x1+"AM"
if(x1=="8:16:0 AM"){alert(x1);}
}
document.getElementById('ct').innerHTML = x;
tt=display_c();
}
</script>
</head>
<body onload=display_ct();>
<span id='ct' ></span>
</body>
</html>