...

View Full Version : user date & time



BigDerek
10-01-2012, 03:47 PM
Hi there, I have these scripts to simply get the users computer date and time:


<script type="text/javascript">
var localuserdate = new Date()
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
document.write((localuserdate.getDate()) + (" ") + (monthname[localuserdate.getMonth()]))
</script>


<script type="text/javascript">
var localusertime = new Date()
var minutes = (localusertime.getMinutes())
if (minutes < 10){minutes = "0" + minutes}
document.write((localusertime.getHours()) + (":") + (minutes))
</script>

How can I turn these values into strings that I can access from php?

I know there is a php/javascript issue whereas the php runs before the javascript, but surely there is a way to get these values rendered to a string prior to the php script being run??? Or can I use some sort of 'reload' script to get the values into the php...

Any ideas will be greatly appreciated as I'm starting to go mental over this...

(and yes, I do need the users time and date rather than server, as this project works on this basis...)

Thanks in advance

devnull69
10-01-2012, 03:50 PM
You can use Ajax techniques to communicate from Javascript to PHP without refreshing the complete page.

BigDerek
10-01-2012, 03:59 PM
Thanks devnull. I have no knowledge whatsoever about ajax. Could you possibly give me a pointer. It would be awesome to solve this problem somehow and then I can move on with my life...

How can I output those values from javascript to php via ajax?

Cheers

devnull69
10-01-2012, 04:26 PM
Simple example ... beyond that you could use Google and (of course) the forum search engine which will lead you to 'bazillions' of examples:



var xmlhttp = new XMLHttpRequest();
var result;
var parameter = 'userdate=' + encodeURIComponent(localuserdate);
xmlhttp.open('POST', 'path/to/your.php', true);
xmlhttp.onreadystatechange = function() {
// what happens when request is finished (readyState is 4)?
if(xmlhttp.readyState==4 && xmlhttp.status==200) {
result = xmlhttp.responseText;
// here you can do whatever you want with result
}
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(parameter);

You can then read the parameter in PHP using $_POST['userdate']. Everything you output from PHP will end up in the Javascript variable 'result'

BigDerek
10-01-2012, 06:32 PM
Ok, I don't think I'm getting anywhere fast here... I've got an external php file called datetime.php which contains the javascript which correctly creates the user date & time:


<script type="text/javascript">
var localuserdate = new Date()
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var localUserDateNOW = ((localuserdate.getDate()) + (" ") + (monthname[localuserdate.getMonth()]))
document.write((localuserdate.getDate()) + (" ") + (monthname[localuserdate.getMonth()]))
</script>

<script type="text/javascript">
var localusertime = new Date()
var minutes = (localusertime.getMinutes())
if (minutes < 10){minutes = "0" + minutes}
var localUserTimeNOW = ((localusertime.getHours()) + (":") + (minutes))
document.write((localusertime.getHours()) + (":") + (minutes))
</script>

then in my main php page I have:

<script language="Javascript">

var xmlhttp = new XMLHttpRequest();
var result;
var parameter = 'userdate=' + encodeURIComponent(localuserdateNOW);
xmlhttp.open('GET', 'datetime.php', true);
xmlhttp.onreadystatechange = function() {
// what happens when request is finished (readyState is 4)?
if(xmlhttp.readyState==4 && xmlhttp.status==200) {
result = xmlhttp.responseText;
// here you can do whatever you want with result
}
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(parameter);
</script>

<a href="#?date=<?php $_GET['userdate'] ?>"><img src="button.jpg" width="55" height="52" /></a>

which includes a button where I'm trying to GET the date variable inserted from the ajax call...

I think I'm going mental... does this make any sense? I'm finding it very difficult to get my head 'round this :(

devnull69
10-01-2012, 08:39 PM
So obviously you strictly want to get the user's date/time into the same PHP page that you used to generate the Javascript to read the date/time in the first place? Why? What do you want to do with the time on PHP side? If you just want to send it back to PHP to view it on the page, you wouldn't have to send it back, you could just show it ...

BigDerek
10-01-2012, 09:19 PM
Yep, that's exactly it. I don't want to display the user's date and time at all. I do, however need to include the user's date and time as php strings in href links at the moment they click. This will also be used to record the same info to the database...

I know this sounds strange, but it's what I need to achieve...

I'm sure it's possible to do as the javascript is doing the function correctly... I just need the ajax and php do reflect the info in the form of a string at the moment of the click...

xelawho
10-01-2012, 11:02 PM
why does it have to go back to the php and then back to the page? Can't you just grab the time at the moment of the click using js (but is it really that useful, being that I could set my machine's time to 12 October 1980 if I wanted) and append it to the href?

BigDerek
10-02-2012, 11:40 AM
I'm open to any suggestions as to how to get this working. If I can get the date/time at the moment of the click as a string, I can then use that to do the navigation and database entries...

I know this sounds crazy, but it is really important and I really appreciate your help getting this to work guys... Thanks

Philip M
10-02-2012, 12:05 PM
When the users clicks on whatever it is, call the function


<script type="text/javascript">

function getDateTime() {
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var d = new Date();
var curr_date = d.getDate();
var cm = d.getMonth(); // months in Javascript are 0-11
var curr_month = monthname[cm];
var curr_year = d.getFullYear();
var curr_hrs = d.getUTCHours(); // assuming you want the time as UTC, not local time
if (curr_hrs<10) {curr_hrs = "0" + curr_hrs}
var curr_mins = d.getUTCMinutes();
if (curr_mins<10) {curr_mins = "0" + curr_mins}
var curr_secs = d.getUTCSeconds();
if (curr_secs<10) {curr_secs = "0" + curr_secs}
var x = curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hrs+ ":" + curr_mins +":" + curr_secs;
alert (x);
}

</script>
Naturally you can format the resultant string as you wish to your requirements.

But as has been pointed out, it is reliant on the date/time as specified in the user's browser, which could be very inaccurate.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum