Danieloplata
Danieloplata

Reputation: 222

Converting a dateTime Object to string

I'm having trouble converting a date Time object to a string, so I can pass it through a MySQLi prepared statement which specifies data types.

The error I'm receiving is:

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

The line this error refers to is the execution of my prepared statement. I still receive this error despite many attempts at converting the Object to a string. Below I will paste the main 3 ways I've tried to tackle this issue.

1 - Using a casting operator:

$total_hours_calc = $time_diff->format('%h') + ($time_diff->format('%i'))/60;
$total_hours = (string)$total_hours_calc;

2 - Using Print_r:

$total_hours_calc = $time_diff->format('%h') + ($time_diff->format('%i'))/60;
$total_hours = print_r($total_hours_calc, true)

3 - Concatenation workaround:

$total_hours_calc = $time_diff->format('%h') + ($time_diff->format('%i'))/60;
$total_hours = $total_hours_calc . "";

All these attempts return the error stated at the beginning of this question.

Any help, solutions or magic PHP functions unknown to those who have yet to achieve a black belt in the art of PHP - are greatly appreciated.

[Requested] The Prepared statement -

$sql = "INSERT INTO shift ".
       "(uniqueid, shift_date, start_time, end_time, total_hours, rate_of_pay, addedBy, paidRate, totalPaid) ".
       "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = $connection->prepare($sql);
$stmt->bind_param('sssssssss', $uniqueid, $shift_date, $start_time, $end_time, $total_hours, $rate_of_pay, $addedBy, $paidRate, $totalPaid); 
$stmt->execute();

The line referred to in all errors is: $stmt->execute();

Upvotes: 0

Views: 7548

Answers (5)

Dev'Hamz
Dev'Hamz

Reputation: 488

You can use format method of the DateTime class :

$date = new DateTime('2014-01-01');
$result = $date->format('Y-m-d H:i:s');

If format fails , it will return FALSE :

if ($result) { // OK
   echo $result;
} 
else { // if format failed
  echo "Unknown Time";
}

Upvotes: 0

Dag Sondre Hansen
Dag Sondre Hansen

Reputation: 2499

According to the php documentation DateTime does not natively implement the __toString() method, which explains the exceptions you keep getting. You may extend DateTime and implement __toString yourself, provided you are using php 5.2 or later.

class DateTimeExt extends DateTime
{
    public function __toString()
    {
        return $this->format("Y-m-d H:i:s");
    }
}

Replace all instances of DateTime used in the bind_param call with DateTimeExt and you should be good.

Upvotes: 7

gapple
gapple

Reputation: 3474

I think you're looking in the wrong place by focusing on $total_hours, which is a already a string (or an integer that can be converted to a string)

In the snippet you linked, you're changing the variables $start_time and $end_time from strings to DateTime objects, which would throw the indicated errors when you later try to use them in the prepared statement.
See this snippet, which prints the diff value correctly, but then errors when printing the DateTime object $start_time: https://eval.in/145608

I can think of two possible resolutions:


Use another variable for the string value of $start_time and $end_time in the prepared statement

$start_time = "9:00";
$end_time = "17:30";

$start_time = new DateTime($start_time);
$end_time = new DateTime($end_time);
$time_diff = date_diff($start_time,$end_time);

$start_time_string = $start_time->format('Y-m-d H:i:s');
$end_time_string = $end_time->format('Y-m-d H:i:s');

$stmt->bind_param('sssssssss', $uniqueid, $shift_date, $start_time_string, $end_time_string, $total_hours, $rate_of_pay, $addedBy, $paidRate, $totalPaid); 

Create a subclass of DateTime that implements __toString(), and utilize it instead.

class DateTimeDb extends DateTime {
  public function __toString() {
    return $this->format('Y-m-d H:i:s');
  }
}

$start_time = "9:00";
$end_time = "17:30";

$start_time = new DateTimeDb($start_time);
$end_time = new DateTimeDb($end_time);
$time_diff = date_diff($start_time,$end_time);

$stmt->bind_param('sssssssss', $uniqueid, $shift_date, $start_time, $end_time, $total_hours, $rate_of_pay, $addedBy, $paidRate, $totalPaid); 

Upvotes: 2

d.raev
d.raev

Reputation: 9546

Parse the values to INT before summing them

$total_hours_calc = (int)$time_diff->format('%h') + ((int)$time_diff->format('%i'))/60;

And quote the result date as string, before using it

$total_hours = "'".$total_hours_calc."'";

There may be a better way to quote the last value.. depending on your implementation,
but $int ."" is usually not enough;

Upvotes: 0

Axel Amthor
Axel Amthor

Reputation: 11096

you can't add two strings

  $total_hours_calc = intval($time_diff->format('%h'),10) + intval($time_diff->format('%i'),10)/60;

Upvotes: 0

Related Questions