There are functions written in JS:

function DateTimeToUnix(d) { var r = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), 00, 00, 00)); var n = (r.getTime()/1000); return n; } function rezerveNCd(code) { var str = rezerveCd(code); var c = 0; var char = ""; var i = 1; var result = ""; var s = ""; for (i=0;i<str.length-1;i++) { char = str.charAt(i); c = char * (i+1); char = c; s = char.toString(); s = s.charAt(s.length-1); result = result + s; } return result; } function rezerveCd(code) { var d = new Date() var unixtime = DateTimeToUnix(d); var i = 0; var str = unixtime.toString(); var n = 1; code = code + 127; var result = str; for (i=0;i<=str.length-1;i++) { n = str.charAt(i)*code; result = result+n; } return result; } 

Trying to write the same thing in PHP:

 function mkTimestamp($year,$month,$day, $hours=00,$minutes=00,$seconds=00){ date_default_timezone_set('UTC'); return mktime($hours,$minutes,$seconds, $month,$day,$year); } function char_at($str, $pos) { return $str{$pos}; } function rezerveCd($code) { $d = mkTimestamp(date("Y"), date("m"), date("d")); $i = 0; $stre = (string)$d; $n = 1; $code = $code + 127; $result = $stre; for ($i = 0; $i <= count($stre) - 1;$i++) { $n = char_at($stre, $i) * $code; $result = $result + $n; } return $result; } function rezerveNCd($code) { $stre = rezerveCd($code); $c = 0; $i = 1; for ($i = 0; $i < count($stre) - 1; $i++) { $char = char_at($stre, $i); $c = $char * ($i + 1); $char = $c; $s = (string)$char; $s = char_at($s, (count($s) - 1)); $result = $result + $s; } return $result; } 

When you call on JS console.log(rezerveCd(60)) and console.log(rezerveNCd(60)) , it gives the correct values. When you try echo rezerveCd(60); gives an incomplete value, and echo rezerveNCd(60); gives nothing. What could be the problem? I would be grateful for the help! PS value of console.log(rezerveCd(60)) - 15294528001899453781701756945378151200 value echo rezerveCd(60); - 1529452987 echo rezerveCd(60); - 1529452987

  • The problem is that instead of having your code run, you should write here immediately what someone would do for you - madfan41k

1 answer 1

You confuse concatenation in js with addition in php.

Almost literal translation:

 function rezerveCd($code) { $str = strval(time()); $code += 127; $result = $str; for ($i=0; $i < strlen($str); $i++) { $n = $str{$i}*$code; $result .= $n; } return $result; } echo rezerveCd(60); // 152948781718793537416837481496130914961871309 
  • Thank you very much! Yes, once again read and dopper, thanks for the help! - funforlifefix