Sir I have these codes

$last_date="";
$last_vno="";

$queryx = "SELECT MAX(t1.date)as mxdate,MAX(t1.vou_no)as mxvno,max(d1)as d1
FROM vouchers t1
WHERE t1.date = (SELECT MAX(t2.date)as date FROM vouchers t2 where vou_type='CP' and dr_amount>0)";

$resultx=sqlsrv_query($con,$queryx)or  die("Error". sqlsrv_errors($con)) ;
if ($resultx){
    while ($rowx = sqlsrv_fetch_array ($resultx)){
    $last_date=$rowx['mxdate'];
    $last_vno=$rowx['mxvno'];
    $myparty=$rowx['d1'];
    }
}else{
    echo "no data found";
    exit;
} 

The query work fine, no issue

nOW i want to use $last_date in an other qeury like this

$query = "SELECT * FROM vouchers where vou_type = 'CP' and date='".$last_date."' and vou_no='".$last_vno."'";

but it says:

Catchable fatal error: Object of class DateTime could not be converted to string in ...

Please help

Recommended Answers

All 2 Replies

$query = "SELECT * FROM vouchers where vou_type = 'CP' and date='".$last_date->format('Y-m-d H:i:s')."' and vou_no='".$last_vno."'";

This should sort it, but I've found in my experience that the easiest way to manage dates and timezones in a database with php and javascript is as integers - INT(11) - unix timestamps. Use moment.js for conversion and display, and store everything as UTC.

Member Avatar for diafol

I'm assuming this first query is returning ONE row, so not sure why using a while loop. Also this is just quote hell:

$query = "SELECT * FROM vouchers where vou_type = 'CP' and date='".$last_date."' and vou_no='".$last_vno."'";

Try:

$query = "SELECT * FROM vouchers where vou_type = 'CP' and date = '$last_date' and vou_no = '$last_vno'";

Is date a reserved word in SQLSRV? If so, use...

$query = "SELECT * FROM vouchers where vou_type = 'CP' and `date` = '$last_date' and vou_no = '$last_vno'";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.