Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2012, 02:47 PM   PM User | #1
BigDerek
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
BigDerek is an unknown quantity at this point
user date & time

Hi there, I have these scripts to simply get the users computer date and time:

Code:
<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>
Code:
<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
BigDerek is offline   Reply With Quote
Old 10-01-2012, 02:50 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You can use Ajax techniques to communicate from Javascript to PHP without refreshing the complete page.
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
BigDerek (10-01-2012)
Old 10-01-2012, 02:59 PM   PM User | #3
BigDerek
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
BigDerek is an unknown quantity at this point
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
BigDerek is offline   Reply With Quote
Old 10-01-2012, 03:26 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Simple example ... beyond that you could use Google and (of course) the forum search engine which will lead you to 'bazillions' of examples:

Code:
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'
devnull69 is offline   Reply With Quote
Old 10-01-2012, 05:32 PM   PM User | #5
BigDerek
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
BigDerek is an unknown quantity at this point
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:

Code:
<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:
Code:
<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
BigDerek is offline   Reply With Quote
Old 10-01-2012, 07:39 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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 ...
devnull69 is offline   Reply With Quote
Old 10-01-2012, 08:19 PM   PM User | #7
BigDerek
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
BigDerek is an unknown quantity at this point
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...
BigDerek is offline   Reply With Quote
Old 10-01-2012, 10:02 PM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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?
xelawho is offline   Reply With Quote
Old 10-02-2012, 10:40 AM   PM User | #9
BigDerek
New to the CF scene

 
Join Date: Oct 2012
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
BigDerek is an unknown quantity at this point
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
BigDerek is offline   Reply With Quote
Old 10-02-2012, 11:05 AM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
When the users clicks on whatever it is, call the function

Code:
<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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-02-2012 at 11:11 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:10 PM.


Advertisement
Log in to turn off these ads.