Convert text field to DateTime in PHP

1

I want to make a query to base de datos in sql server where the field is of type datetime. At the moment of doing this in PHP it shows me the following error:

  

Warning: strtotime () expects parameter 1 to be string, object given in C: \ xampp \ htdocs \ sana_php \ kardex.php on line 44

     

Warning: strtotime () expects parameter 1 to be string, object given in C: \ xampp \ htdocs \ sana_php \ kardex.php on line 44

<?php session_start(); 

if (isset($_SESSION['usuario'])) {
    require 'vistas/kardex.view.php';
}else{
    header('location: servicios.php');
}
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $buscar=filter_var($_POST['buscar'], FILTER_SANITIZE_STRING);
            $fecha1=date('d-m-Y', strtotime($_POST['fecha1']));
            print_r($fecha1);
            $fecha2=date('d-m-Y', strtotime($_POST['fecha2']));
        $serverName="(LOCAL)";
        $connectionInfo=array("Database"=>"dbsav300", "UID"=>"sa", "PWD"=>"admin01");
        $conn=sqlsrv_connect($serverName, $connectionInfo);

        if ($conn) {
            # code...
        }else{
            echo "Conexion no pudo establecerse";
            die(print_r(sqlsrv_errors(), true));
        }

        $query="SELECT art_Clave, art_Costo, kar_Fecha, kar_Cantidad, kar_Origen, alma_Existencia FROM tArticuloKardex WHERE art_Clave='".$buscar."' AND (kar_Fecha BETWEEN '".$fecha1."' AND '".$fecha2."')";
        print_r($query);

        $resultado=sqlsrv_query($conn, $query);
        //se desplegaran los resultados en la tabla
        echo "<table border=1>";
        echo "<tr>";
        echo "<th>SKU</th>";
        echo "<th>COSTO</th>";
        echo "<th>FECHA DE MOVIMIENTO</th>";
        echo "<th>PIEZAS</th>";
        echo "<th>TICKET</th>";
        echo "<th>EXISTENCIA FINAL</th>";
        echo "</tr>";

        while($row=sqlsrv_fetch_array($resultado)){
            echo '<tr>';
            echo '<td>'.$row['art_Clave'].'</td>';
            echo '<td>'.$row['art_Costo'].'</td>';
            echo '<td>'.date('d-m-Y', strtotime($row['kar_Fecha'])).'</td>';
            echo '<td>'.$row['kar_Cantidad'].'</td>';
            echo '<td>'.$row['kar_Origen'].'</td>';
            echo '<td>'.$row['alma_Existencia'].'</td>';
            echo '</tr>';           
        }
        echo "</table>";
    }

?>

And it shows me on the screen what follows

    
asked by Guillermo Ricardo Spindola Bri 25.07.2016 в 20:07
source

3 answers

1
$karFecha = $row['kar_Fecha'];
echo '<td>'.$karFecha->format("Y-m-d").'</td>'
    
answered by 27.08.2016 в 00:25
0

Bearing in mind that what you have is a DateTime object, what you need is to format it before showing it, using format () :

$karFecha = $row['kar_Fecha'];
echo '<td>'.$karFecha->format("Y-m-d").'</td>';
    
answered by 25.07.2016 в 20:15
0

Here is an example:

$date = new DateTime('2000-01-01');
$string_date = $date->format('Y-m-d H:i:s');
    
answered by 25.07.2016 в 20:16