Convert data javascript to work in PHP

Asked

Viewed 296 times

1

I’m trying to capture the file date via javascript and send it to PHP.

Motive?

I want the date of the last file change.

$('#last_modified').val(arquivo[0].lastModified);

Variable file is:

<input type="file" name="teste" onchange="pegaArquivo(this.files)">

Which returns me "1423493594000" for the date: Mon Feb 09 2015 12:53:14 GMT-0200 (BRST)

However, when PHP reads this date it always returns the same: 12/31/1969

In php I do:

echo date ("d/m/Y", filectime($_POST['last_modified']));

Could you help me?

  • 3

    Javascript works in milliseconds and PHP in seconds, 1423493594000 in javascript gives Feb 2015, in php gives there to 2028 if not divided by 1000... how you’re getting into PHP?

  • 1

    Just like @Sergio said. This topic is a bit like your question, take a look and see if it helps you: http://stackoverflow.com/questions/10837022/convert-php-date-into-javascript-date-format

  • Excuse me, I updated the post: echo date ("d/m/Y", filectime($_POST['last_modified']));

  • 1

    Raul, take a look at: http://www.html5rocks.com/en/tutorials/file/dndfiles/ right at the start he applies exactly what you would like. Show modification date. In this sector Using form input for Selecting. Take a test, upload and see the result :)

  • 1

    Thanks @Sergio, that’s right, split by 1000.

  • Thanks @Renilsonandrade, helped a lot too. =)

  • @Rafaelwithoeft I will read the link calmly, maybe it helps even more =) By the way, how to pronounce your last name?

  • @Raul3k " Viper "; I believe to be of German origin... kkkkk

  • @Raul3k I haven’t seen in your code where the bug was, you didn’t put it where it came from arquivo[0].lastModified. But if you found the solution you can delete the question or ask an answer.

  • @Rafaelwithoeft I imagine you spelling by phone rs I just posted the solution along with the code =)

  • Obs: onchange="pegaArquivo(this.files)" Look at the onchange, this is where I’m getting it =)

  • @Raul3k Yes, it’s always a difficulty, I don’t even try to talk anymore, I start spelling. kkkkk

Show 7 more comments

1 answer

1


My mistake was trying to convert the time passed by javascript straight to PHP.

After the comments on the question itself,I made the requested changes and is now working.

Follow the code working for anyone who wants to see:

<script>
    function pegaArquivo(arquivoSelecionado) {
        if(arquivoSelecionado[0]){
            $('#last_modified').val(arquivoSelecionado[0].lastModified);
        }
    }
</script>

<input type="hidden" id="last_modified" name="last_modified" />
<input type="file" name="teste" onchange="pegaArquivo(this.files)"> <input type="submit">

In PHP:

echo 'Data do arquivo: <br>';
$dateInfo = getdate($_POST['last_modified']);
echo date ("d/m/Y", (int) $dateInfo[0]/1000);

The trick was to divide the javascript team by 1000 since Javascript works with milliseconds and PHP with seconds.

Thank you all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.