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.

Jakub Zak
  • 1,212
  • 6
  • 32
  • 53
  • 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 03 '12 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 03 '12 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 03 '12 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 03 '12 at 19:44

3 Answers3

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

Community
  • 1
  • 1
gpasci
  • 1,420
  • 11
  • 16
  • 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 03 '12 at 19:54
  • follow the link in my answer...hope it helps – gpasci Mar 03 '12 at 19:58
  • What would be an example form selector? Say I had a form like... `
    `
    – Amyunimus Mar 23 '12 at 21:52
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

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);
clem
  • 3,345
  • 1
  • 22
  • 33
  • 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 03 '12 at 19:37