Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You can use Ajax techniques to communicate from Javascript to PHP without refreshing the complete page.

  • Users who have thanked devnull69 for this post:

    BigDerek (10-01-2012)

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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'

  • #5
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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 ...

  • #7
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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?

  • #9
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    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.
    Last edited by Philip M; 10-02-2012 at 12:11 PM.

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •