0

I need to assign javascript client Date (examp. - 2012/02/03 16:00:00) to php variable. Any idea how? I was trying to use this lines and changing them in million different ways. But I just cant get it.

Today = new Date();
var date = ????
var date = "<?= $date ?>";

I solved it this way:

<input id="date" type="hidden" name="date">
<script type="text/javascript">
     document.getElementById('date').value = Date();
</script>

But thank you very much all.

4
  • 2
    You want the client date in your php code? Not possible in the way you are doing it. Pay attention at how the client-server model works, you can't mix client logic and server logic that way. Commented Mar 3, 2012 at 19:37
  • Yes, now when I think about it's true. But if I wanted javascript date variable put into form, into hidden input how would I do that? Commented Mar 3, 2012 at 19:40
  • You can implement an ajax solution to make a call to your php script to pass the current client time to your server. Commented Mar 3, 2012 at 19:42
  • But if this form with hidden input is not in php but in html do I still need to use ajax? Commented Mar 3, 2012 at 19:44

3 Answers 3

1

Add an input in your form

<input type="hidden" name="clientDate">

if you are using jquery add this to set the client date input when the user submits the form

$(YOUR_FORM_SELECTOR).on("submit", function() {
  $("[name=clientDate]").val(new Date());
});

If you want to go with vanilla javascript follow this answer

Sign up to request clarification or add additional context in comments.

3 Comments

To be honest I never used jQuery so I do not really know how? I just now am thinking about getting client side date(). Assigned it to html form to hidden input. Pass it by post to another page where php will take it as $_POST["#"].
follow the link in my answer...hope it helps
What would be an example form selector? Say I had a form like... <form name="signup" id="signup1" method="post">
0

you can't simply assign a javascript variable to a php variable. php runs on the serverside and is executed before javascript.
you can however submit an ajax call with the value of your javascript variable to a php script.
you might wanna have a look at jquery's post function.

$.post("test.php", { yourDate: date } );

in your PHP script you'll be able to access the date with $_POST['yourDate'] you can also use a form and a hidden field as you say in your comment.
in this case you can use (assuming you're using jQuery)

$('#id_of_input').val(date);

1 Comment

ok I'm gonna ask different way than. I have a form, I have a hidden input to which I would like to assign javascript variable. Is that possible? Concretely time.
0

This will convert js variable to php variable and php variable to js variable

<script>
function jstophp(){


var javavar=document.getElementById("text").value;  

document.getElementById("rslt").innerHTML="<?php 
$phpvar='"+javavar+"'; 
echo $phpvar;?>";
}

function phptojs(){

var javavar2 = "<?php 

$phpvar2="I am php variable value";
echo $phpvar2;

?>";
alert(javavar2);
}

</script> 
<body>
<div id="rslt">
</div>


<input type="text" id="text" />
<button onClick="jstophp()" >Convert js to php</button>
<button onClick="phptojs()">Convert php to js</button>

PHP variable will appear here:
<div id="rslt2">
</div>

</body>

Demo: http://ibence.com/new.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.