The following code gets the date in the form 6/4/2016
<script>
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var currentDate = (month + "/" + day + "/" + year)
document.write(month + "/" + day + "/" + year)
document.write(currentDate)
</script>
I've tried several different ways to submit the date as a hidden field in a form to use as the primary key in a SQL database.
<input type="hidden" name="DateSubmitted" value=($month + "/" + $day + "/" + $year)>
^ shows $month as what was submitted in the array
<?php $currentDate = date('Y-m-d'); ?>
<input type="hidden" name="mDateJoined" value="<?php echo $currentDate; ?>">
^is blank in the array
<input type="hidden" name="DateJoined" value=currentDate>
^displays currentdate as the text submitted.
Rather than submit the date from the form to the php page, I just included
$currentDate = date('Y-m-d');
on the php page for sql insertion.
I have to assume you want to read the clock of the client computer, which might be wrong, not just have the date and time the form was submitted? If the latter is the case use the suggestion of assemblyhelpneededplease, it is more reliable.
The mistake you seem to make is to mix HTML, Javascript, and PHP. These three are quite different beasts. You should have a clear idea about what is which. This PHP script will get the date from the computer of your visitor:
<?php
echo '<script>
var currentTime = new Date();
var month = currentTime.getMonth()+1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var currentDate = month+"/"+day+"/"+year);
document.write("<input type=\"hidden\" name=\"mDateJoined\" value=\""+currentDate+"\">");
</script>';
So this is a PHP script outputting Javascript which generates HTML. Complicated. Better follow the suggestion of assemblyhelpneededplease!
Related
how can I remove days from input type date? I added default data:
<input required class="data_inizio" type="date" name="inizio" value="2018-01-31">
But I would to remove previous days before that..
You could use the min attribute, to only allow future dates. Like so: <input required class="data_inizio" type="date" min="2018-01-31" name="inizio" value="2018-01-31">. You will only need to replace the min value with a dynamic value which represents the current date. You could use something like php for that: .
So it would become:
<input required id="date-input" class="data_inizio" type="date" min="<?=date('Y-m-d', time())?>" name="inizio" value="2018-01-31">`.
You can find the browser support over here: Browser_compatibility
Or setting the date using javaScript:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = yyyy + '-' + mm + '-' + dd;
document.getElementById("date-input").setAttribute('min', today);
I want to compare date and with date in user timezone after user's login.
I get user's timezone with php.
now How to I take current date/time in javascript of user's timezone current time.
#everyone answered:
I don't need current local system timezone or offset in seconds.
I need users current time with dynamic time I get in php.
For example:
I have 'Asia/Kolkata' or 'America/Denver' or whatever that will be dynamic by php.
Now I can save it to hidden value so that I can get it with Javascript
and than I need current date of this timezone same like Javascript gives date object.
JavaScript uses the system's time zone. You can get the offset via GetTimezoneOffset
<script>
var date=<?php echo date("Y/m/d")?>
</script>
this might help you
Try like this
<script>
var date = '<?php echo date("Y/m/d");?>';
alert(date);
</script>
deeply you can also try like this
var today = new Date();
var localoffset = -(today.getTimezoneOffset()/60);
var destoffset = -4;
var offset = destoffset-localoffset;
var d = new Date( new Date().getTime() + offset * 3600 * 1000);
alert(d);
there you can get
Try this:
// date() function
var currDate = new Date()
var n = currDate.getTimezoneOffset();
You can send UTC string
strtotime("2013-02-08 00:00:00");
After that you would have seconsd, that you can convert simple to JS Date:
var utcString = 1360263600 /*AJAX result must be here*/;
new Date(utcString*1000);
You can use Date Object of JavaScript to get user's local system..
for example you can use following:
var d = new Date();
var time = d.getTime();
alert(time);
this will alert the the number of milliseconds since midnight.
You can send this time to your server and compare the both.
For more functions and details you can visit JavaScript Date Object
I am from Taiwan and it returns "+8" for me.
Working example
JS
function timezone() {
var offset = new Date().getTimezoneOffset();
var minutes = Math.abs(offset);
var hours = Math.floor(minutes / 60);
var prefix = offset < 0 ? "+" : "-";
return prefix+hours;
}
$('#result1').html(timezone());
HTML
<div id="result"></div>
Result
+8
I would recommend to use moment.js with moment timezone.
Then you can do something like:
moment().tz("Asia/Kolkata").format();
basically I know more or less how to do it but would like to know if there is any better way?
I have the following variables in PHP
$day; $month; $year;
The ones above have values from exploding a php date string.
Below is PHP plugin function which states the date for countdown.
<script type="text/javascript">
$(function () {
var austDay = new Date();
austDay = new Date(2013, 12, 22);
$('#defaultCountdown').countdown({until: austDay});
});
</script>
I would like to pass the date day/month/year variables into that function from PHP
how can I do it, when I tried to attach to the javavariable and put that variable in place of the date part, it didnt work.
Thanks for all help
var day = <?php echo $day ?>;
var month = <?php echo $month ?>;
var year = <?php echo $year ?>;
$(function () {
var austDay = new Date();
austDay = new Date( year, month, day );
$('#defaultCountdown').countdown({until: austDay});
});
There's a couple of ways you could skin this.
Fetch the values via $.ajax with php returning the values as a jsonified array (echo json_encode($my_values))
If the page generating the html is a php page then just new Date();
Place the values into hidden form fields anywhere on the page or into data-day, data-month, data-year attributes of a relevant object on the page and fetch the values using jquery
day = $('#hiddenfield_day').val(); //put the var day into the day field of new date, etc
Hope this helps.
Change this line:
austDay = new Date(<?= $year ?>, <?= $month ?> , <?= $day ?>);
That said, keep in mind that Javascript's new Date() month param takes a number in the 0 - 11 range.
I have a simple textfield with the type defied to be as date which attached with a jquery datepicker(). Now i have set the dateFormat() to be dd-mm-yy. All this works fine. But as i know MySql requires date in yyyy-mm-dd. So i was just wondering if i can convert that date format either using php or javascript to enter into the database.
E.g var date = $('#order_date').val(); gives value 26-2-2012. I need to convert this into 2012-02-26.
Note: There is also a 0 being added to the month.
Use PHP:
$date = $_POST['date']; // e.g. 06-08-2011
$mysql_date = date('Y-m-d', strtotime($date));
or
$date = explode('-', $_POST['date']);
$mysql_date = $date[2].'-'.$date[1].'-'.$date[1];
use $mysql_date to insert into the database.
You can do in javascript by parsing the input and loading it into a Date() object, and then using the getMonth, getDate, and getFullYear functions, as follows:
//Assume input is dd-mm-yyyy
var dateParts = $('#order_date').val().split("-");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
//format using getMonth(), getDate() and getFullYear() methods)
var formattedMonth = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
var formattedDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var output = date.getFullYear() + '-' + formattedMonth + '-' + formattedDate;
You can see more at this excellent StackOverflow post
did u try date.format("yyyy-mm-dd"); ?
u can also use "isoDate" in place of "yyyy-mm-dd"
and if u are using mysql u can use DATE_FORMAT(date,'%Y-%m-%d')
Is it possible through Jquery/Javascript that I can validate a date with the following conditions
example date : 10/10/2011
Conditions : Not in the past (accept
today onwards) Not more than 18 months
(this can be variable)
Because the ways I found out was
var e = new Date();
Alert(e.getMonth() + 2);
but this is not what I wanted
you might want to have a look at date.js
it's a great library for manipulating dates in javascript.
http://www.datejs.com/
here's the documentation:
http://code.google.com/p/datejs/wiki/APIDocumentation
it can also do the comparisons for you!
have a look at Date.compare and Date.addMonths
You can use JQuery's Datepicker, it has validation included and is pretty nice overall.
It can be done like this:
var toValidate = new Date('10/10/2011');
var months = 18;
var now = new Date();
var minDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var maxDate = new Date(now.getFullYear(), now.getMonth() + months, now.getDate());
var valid = minDate < toValidate && toValidate < maxDate;