Reputation: 9522
I am trying to change the hour in my DateTime, and I'm doing it right how the docs say. However there must be something I'm not understanding because when calling that function I'm getting this error Call to a member function setTime() on a non-object
.
This is my code:
$date = date("Y-m-d H:i:s");
$date_in_secs = strtotime($date);
$new_date = $date_in_secs + $delivery_duration + $distribution_time;
$date = date('l d F H:i Y', $new_date);
if (intval(date('H', $new_date)) > 21 || intval(date('H', $new_date)) < 8) {
$date->setTime(8, 0, 0);
$date->add(new DateInterval('P1D'));
}
Any clue on how I can change my date hour and add one day?
Upvotes: 0
Views: 4732
Reputation: 397
What is the contents/type of $delivery_duration
and $distribution_time
?
Also why are you doing this?
$date = date("Y-m-d H:i:s"); // Convert DateTime to String
$date_in_secs = strtotime($date); // Convert String to Timestamp
Why not just use DateTime? Much cleaner...
$date = new DateTime;
$date->modify('+200 seconds'); // Increase with 200 seconds from $delivery_duration for example
if (intval($date->format('H')) > 21 || intval($date->format('H')) < 8) {
$date->setTime(8); // Set Hour to 8
$date->modify('+1 day'); // Increase day by 1
}
$dateString = $date->format('Y-m-d H:i:s');
Upvotes: 1
Reputation: 476
From the doc :
public DateTime DateTime::setTime ( int $hour , int $minute [, int $second = 0 ] )
You are putting a string when this is waiting for int separed by commas try this :
$date->setTime(8,0,0');
Upvotes: 0