Fatal error when trying to format output using date_diff() - php

I'm using PHP 5.3.6 and when I try to run the code bellow I get the following error: "
Fatal error: Call to a member function format() on a non-object in ...".
function diferenta_date($data_inceput, $data_sfarsit){
$interval = date_diff(date_create($data_inceput), date_create($data_sfarsit));
$output = $interval->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s");
$return_output = array();
array_walk(explode(',', $output), function($val, $key) use(&$return_output) {
$v = explode(':', $val);
$return_output[$v[0]] = $v[1];
});
return $return_output;
}
What's wrong?

You need to check the return values. The documentation says date_diff() returns:
The DateInterval object representing the difference between the two dates or FALSE on failure.
date_diff() is failing and you are trying to use FALSE as an object.

Related

ReactPHP get_class() expects parameter 1 to be object, string given

I am getting this error code:
PHP Warning: get_class() expects parameter 1 to be object, string
given in /phalcon/vendor/clue/block-react/src/functions.php on line 90
PHP Fatal error: Uncaught UnexpectedValueException: Promise rejected
with unexpected value of type in
/phalcon/vendor/clue/block-react/src/functions.php:89 Stack trace:
0 /phalcon/vendor/clue/block-react/src/functions.php(198): Clue\React\Block\await(NULL, Object(React\EventLoop\StreamSelectLoop),
NULL)
1 /phalcon/app/tasks/RunTask.php(96): Clue\React\Block\awaitAll(NULL, Object(React\EventLoop\StreamSelectLoop))
I'm building my promises with the following code:
$promises[] = $this->getTrades($rule->id, $exchange->id)
->then(
function ($trades) use ($handleTrades) {
foreach ($trades as $trade) {
$handleTrades[] = $trade;
}
}
);
The function is like this:
private function getTrades($rule, $exchange) {
$deferred = new React\Promise\Deferred();
//...
if (empty($trades->count())) {
$deferred->reject("No trades found for rule $rule");
} else {
$deferred->resolve($trades);
}
return $deferred->promise();
}
How do I solve this?
The real reason is Fatal error: Uncaught UnexpectedValueException, not the warning. As you can see the clue/reactphp-block library expects an Exception in your reject function. Try:
$deferred->reject(new \Exception("No trades found for rule $rule"));
As Furgas mentioned above I needed to add the error handling function in the promise.

PHP Fatal error: Uncaught Error: Call to a member function getTimestamp() on boolean

I'm running my PHP CodeIgniter app on Heroku and many users started getting 500 errors just today.
I checked the logs and found multiple entries for:
PHP Fatal error: Uncaught Error: Call to a member function getTimestamp() on boolean
Doing some searching, it seems as though others have encountered problems with getTimestamp(). Are there any alternatives?
Here is the corresponding code:
public function compareDates($date){
$LastFiveDates = $this->getLastFiveDates();
$signUpDate = $this->getUserSignUpDate();
$requested_date = $date;
if($LastFiveDates[0] < $signUpDate){
$signUpDate = $LastFiveDates[0];
}
if($signUpDate == NULL){
$signUpDate = "09-25-2017";
}
$signUpDate_dt = DateTime::createFromFormat("m-d-Y", $signUpDate);
$signUpDate_ts = $signUpDate_dt->getTimestamp();
$requested_date_dt = DateTime::createFromFormat("m-d-Y", $requested_date);
$requested_date_ts = $requested_date_dt->getTimestamp();
if ($signUpDate_ts > $requested_date_ts) {
$this->noAccess();
}
}

Call to undefined method Memcached::getAllKeys() on PHP7

I have this script
$prefix = "feed_";
$keys = #$memcached->getAllKeys();
foreach ($keys as $index => $key) {
if (strpos($key,$prefix) !== 0) {
unset($keys[$index]);
} else {
$memcached->delete($key);
}
}
that return this fatal error: Fatal error: Uncaught Error: Call to undefined method stdClass::getAllKeys().
If I run PHP5.* all works fine. But with PHP7.* it's not the same.
I don't find anything about deprecated function http://php.net/manual/en/memcached.getallkeys.php
How can I resolve this problem?

How can i use this code in PHP 5.2?

This is a part of SpreadsheetReader_XLSX.php from spreadsheet-reader-master that I want to use on PHP 5.2.6 but it always gives an error: Fatal error: Call to undefined method DateTime::add() in ..\spreadsheet-reader-master\SpreadsheetReader_XLSX.php on line 830 and it is not possible to upgrate my PHP version.
$Value = clone self::$BaseDate;
$Value -> add(new DateInterval('P'.$Days.'D'.($Seconds ? 'T'.$Seconds.'S' : '')));
if (!$this -> Options['ReturnDateTimeObjects'])
{
$Value = $Value -> format($Format['Code']);
}
else
{
// A DateTime object is returned
}

PHP parse error when use []

The code is
// Get singleton (first value from row with single value)
static function singleton($arg, $params = false) {
return pg_fetch_row(SQL($arg, $params))[0];
}
The error message is
2014-02-19 12:54:23: (mod_fastcgi.c.2701) FastCGI-stderr: PHP message: PHP Parse error: syntax error, unexpected '[' in /var/www/blockexplorer.com/htdocs/includes/sql.inc on line 69
I think there is a config that can fix it.
This depends on PHP version you are using. If you are using PHP 5.4 or above then your code will not give error otherwise you will have to store the result in a variable and use it.
Reference : PHP 5.4
Look for "Array Dereferencing" here.
Put the result of the function in a variable
static function singleton($arg, $params = false) {
$foo = pg_fetch_row(SQL($arg, $params));
return $foo[0];
}
PHP does not support anonymous arrays. Use an named array instead:
static function singleton($arg, $params = false) {
$row=pg_fetch_row(SQL($arg, $params));
return $row[0];
}

Categories