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.

share|improve this question

59% accept rate
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. – gpasci Mar 3 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? – Jakub Zak Mar 3 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. – gpasci Mar 3 at 19:42
But if this form with hidden input is not in php but in html do I still need to use ajax? – Jakub Zak Mar 3 at 19:44
see my answer.. – gpasci Mar 3 at 19:48
feedback

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
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["#"]. – Jakub Zak Mar 3 at 19:54
follow the link in my answer...hope it helps – gpasci Mar 3 at 19:58
I found about 5min ago same one. :) thank you very much – Jakub Zak Mar 3 at 20:01
What would be an example form selector? Say I had a form like... <form name="signup" id="signup1" method="post"> – Amyunimus Mar 23 at 21:52
$("#signup1"); api.jquery.com/category/selectors – gpasci Mar 23 at 23:40
feedback

You can not assign it directly. You ca do this only by creating a http request. In this case using ajax

share|improve this answer
Yes, it works, I used it in a website I made. It's not perfect but it's nice to use! – Frederick Marcoux Mar 3 at 19:37
1  
@FrederickMarcoux no it doesn't. what you're doing is assigning a PHP variable to js. Jakub asked for the opposite – stratton Mar 3 at 19:39
@FrederickMarcoux You can not assign a JS variable to PHP assuming JS is running under a web browser. – shiplu.mokadd.im Mar 3 at 19:44
Okok, I didn't understood the question correctly... – Frederick Marcoux Mar 3 at 19:52
feedback

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);
share|improve this answer
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. – Jakub Zak Mar 3 at 19:37
I've updated my answer. – stratton Mar 3 at 19:42
not sure why anybody would vote my answer down??? – stratton Mar 3 at 19:42
@stratton I think its the same person who down-voted me. – shiplu.mokadd.im Mar 3 at 19:47
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.