PHP is a server language, that is, the script is sent to the server, it is processed and then returns the output, since javascript is a client language, it is not sent to the server to process, so there is no way to capture in this way as you want, unless you use the value already processed via GET, PUT, POST or by parsing the document itself. How you can solve the problem by sending this via AJAX:
<?php
if(isset($_POST['fin'])) {
$fin = $_POST['fin'];
$tfin = explode(":", $fin);
echo "<pre>";
var_dump($tfin);
echo "</pre>";
die();
}
?>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(function(){
var fin = "02:00:00";
var data = {fin:fin};
$.post('<?php echo $_SERVER["SCRIPT_NAME"]; ?>', data, function(e) {
document.write('<html><body>'+e+'</body></html>');
});
});
</script>
Another way is to use the explode javascript, the function split()
:
<body></body>
<script>
var fin = "02:00:00";
var arrFin = fin.split(":");
var out = '';
for (var i in arrFin) {
out += i + ' <font color="#888a85">=></font>'+
' <small>'+ typeof arrFin[i] +"</small> "+
"<font color=\"#cc0000\">'" +
arrFin[i] +
"'</font> <i>(length="+arrFin[i].length+")</i>\n";
}
var pre = document.createElement('pre');
pre.setAttribute('class','xdebug-var-dump');
pre.setAttribute('dir','ltr');
pre.innerHTML = out;
document.body.appendChild(pre);
</script>
why don’t you use the
split()
js? what is the reason for going to php?– rray
Well, I’m developing a game, but they told me to work with timestamp because then I have a ranking and need to sort in relation to the shortest time
– Gabriel Longatti
Use ajax for this or send this value through a form.
– rray
From JS pro PHP only has a way if it is with Ajax. Your question is not very clear whether it is PHP->JS or JS->PHP...
– brasofilo
Besides, because the function
explode
is not working? Returns an error? What appears there when you make avar_dump
?– KaduAmaral