Error: "Unexpected T_STRING" in PHP

Asked

Viewed 2,266 times

2

I took a function from the Internet to calculate the difference between the dates. I created a class to facilitate, because I will use this function on other screens.

PHP version: 5.2.*

Code calling the class:

  include("funcoes/datahora.php");
  $datahora = new dataHora();

That is the mistake:

Parse error: syntax error, unexpected T_STRING in /home/a6483778/public_html/funcoes/datahora.php on line 1

Follow my PHP code:

<?php // <-- O erro está aqui, na primeira linha!
class dataHora {    
    function data($data){ 
        $data_atual = mktime(); 

        list($ano,$mes,$dia) = explode("-",$data);
        list($dia,$hora) = explode(" ",$dia);
        list($hora,$min,$seg) = explode(":",$hora);

        $data_banco = mktime($hora,$min,$seg,$mes,$dia,$ano); 

        $diferenca = $data_atual - $data_banco; 

        $minutos = $diferenca/60; 
        $horas = $diferenca/3600; 
        $dias = $diferenca/86400; 


        if($minutos < 1){ 
            $diferenca = "há alguns segundos. Mais precisamente: ".$diferenca." segundos";
        } elseif($minutos > 1 && $horas < 1) { 
            if(floor($minutos) == 1 or floor($horas) == 1){ $s = ''; } else { $s = 's'; } 
            $diferenca = "há ".floor($minutos)." minuto".$s;
        } elseif($horas <= 24) { 
            if(floor($horas) == 1){ $s = ''; } else { $s = 's'; } 
            $diferenca = "há ".floor($horas)." hora".$s;
        } elseif($dias <= 2){ 
            $diferenca = "ontem";
        } elseif($dias <= 7){ 
            $diferenca = "há ".floor($dias)." dias";
        } elseif($dias <= 8){
            $diferenca = "há uma semana";
        } else {
            $diferenca = date("d/m/Y",$data_banco);
        } 

        return $diferenca; 
    }    
}
?>

Source code

  • 1

    Can post/highlight the error line and previous, It looks like an unzipped quote.

  • That’s right @lost, probably missing or left a string delimiter. However it is not in the code posted, because it has no syntax error.

  • Updated! @lost

  • Does this code run on a linux server? the file was created in windows?

  • It was created in windows, and I’m not sure if it’s linux server, @lost

  • Create a new file and put phpinfo() this will show the operating system.

  • 2

    Leonardo, the mistake could be encoding, since the file has accents. Try using Notepad++ or some editor and convert it to UTF-8. Or even it may have been damaged in the transfer via FTP. Already tried sending again?

  • I think I know what it is, try using the tag <? at the beginning and tag ?> in the end, instead of <?php at the beginning and ?> at the end, to see if the error goes away.

  • @lost. Yes, it’s Linux!

  • I couldn’t reproduce your mistake, here in my machine worked perfectly.

  • What @utluiz suggested worked, kkkk. I uploaded again and it worked perfectly, I think at the time of transferring, must have damaged the file. Thanks !

Show 6 more comments

2 answers

2


I uploaded again and amazingly it worked. I think at the time of transferring the file, must have damaged.

Thanks for the help!

  • 2

    The FTP protocol has serious problems when thinking about file integrity. In several hosts I have had problems, for example, when sending several files in batch, several of them randomly get corrupted. If your server supports, use a better SSH protocol with a client as Winscp.

0

According to this reply, the problem is in the line breaking caractres that in windows is representing as \r\nand on linux only \n

I fixed it. Writing the code in Windows implied having r n as line break characters, which have not been interpreted correctly on my Linux hosting: I’ve turned all r n to the UNIX standard character line break n and corrected error.

  • In some specific situation this may even cause the problem, but I always compacted my PHP files by removing line breaks without difficulties. Behold here the question file without line breaks.

Browser other questions tagged

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