Error in php code when changing plesk server - php

I have this code:
$ah = date("H:i:s Y-m-d");
$ahseg=mktime($ah);
and this error:
Fatal error: Uncaught TypeError: mktime(): Argument #1 ($hour) must be of type int, string given in /var/www/vhosts/dominio.com/httpdocs/admin/maniobra.php:8 Stack trace: #0 /var/www/vhosts/dminio.com/httpdocs/admin/maniobra.php(8): mktime() #1 {main} thrown in /var/www/vhosts/dominio.com/httpdocs/admin/maniobra.php on line 8
This code was working on other hosting, but when I pass it to the new server that uses plesk it throws me this error

Because of a php version change:
see: https://3v4l.org/she2g
If you want to have a timestamp, use time(), if you want to use mktime(), check the dox, you are giving a string date to the hour (int) parameter:
https://www.php.net/manual/en/function.mktime.php

Regardless of whether it used to work, the error message is quite straight forward: the first argument is supposed to be an int, but you are passing a string.
The documentation for mktime confirms that while it's possible to call it with one argument, that one argument is the hour, not a formatted date time string.
The only reason this used to work is that in earlier PHP versions, the string was "coerced" to an integer, so you were running this:
$ah = date("H:i:s Y-m-d");
$ahseg=mktime((int)$ah);
If the string in $ah is "10:59:00 2022-02-02", then the integer passed to mktime is 10, which happens to be the hour. The rest of the string is thrown away, and other fields are left at their default values ("now").
It's unclear what this code was trying to do, but for an integer representing "now", you can just use time(); for an integer representing some other date, use something like strtotime('2020-01-01 19:45');.

Related

PHP reading API - mandatory parameter not sent

I am getting familiar with working with APIs. I have the following question:
Error is:
Fatal error: Uncaught Larislackers\BinanceApi\Exception\BinanceApiException: [-1102]: Mandatory parameter 'timestamp' was not sent, was empty/null, or malformed. thrown in C:\Users\Francisco\vendor\larislackers\php-binance\src\BinanceApiContainer.php on line 568
Edit: This is the class: https://larislackers.github.io/php-binance/source-class-Larislackers.BinanceApi.BinanceApiContainer.html#276-291
I can notice it's because the TIMESTAMP is mandatory:
https://www.binance.com/restapipub.html#current-open-orders-signed
Can someone provide me an example of how would they fill the timestamp?
This is my code so far:
$orders = $bac->getOpenOrders(['symbol' => 'XVGETH']);
var_dump($orders->getBody()->getContents());
Add a timestamp key to the $params array.
$orders = $bac->getOpenOrders(['symbol' => 'XVGETH', 'timestamp' => time()]);
time() returns the current time. If you want some other time, there are many date-time functions that can be used. It's also just a number that represents the time in seconds, you can perform ordinary arithmetic like time() - 86400 to get the current time yesterday.
See https://www.binance.com/restapipub.html for how the Binance API uses the timestamp option.

DateTime format expects parameter 1 to be string, Object given - Symfony2

I'm having some troubles using the format function in Symfony2 when trying to insert a date interval into a table, for the purpose of setting due dates for created invoices.
Here is what I have:
$today = new \DateTime();
$interval = $today->add(new \DateInterval('P1M'));
$invoice->setDueDate($interval->format('Y-m-d H:i:s'));
However, when I hover over the format parameter in PHPStorm, it tells me that it's expecting a DateTime object not a string, and I get the following error in my profiler:
Error: Call to a member function format() on string
So, I changed the line to this:
$invoice->setDueDate($interval->format(new \DateTime()));
But when I run that, my profiler gives this error:
Warning: DateTime::format() expects parameter 1 to be string, object given
It almost seems like a catch 22 situation! I am really baffled, do I use a string or a DateTime object, because either one fails yet warns me I need to use one or the other..
Any ideas?
I’d guess that $invoice->setDueDate() is the one expecting a DateTime instance. So the line should be $invoice->setDueDate($interval);

php : A non well formed numeric value encountered

i am trying to get the users age, i am getting the corrrect output but with a warning in my html page
Error:
A PHP Error was encountered
Severity: Notice
Message: A non well formed numeric value encountered
Filename: controllers/users.php
Line Number: 45
Code i used is :
$now = time();
$data['dob'] = strftime('%Y', $now)-strftime('%Y',$query['dob']); //it should be showig user age
what could be the reason for the above shown error, Please help
strftime returns a string, you must convert it to number before subtraction.
EDIT: seeing the comment below, you cannot subtract, as dob is not in the same format returned by time(). So, proceed as in the paragraph above.
EDIT2: cleanest way to subtract two dates is DateTime
i converted the string into int , and subtracted the value to get my output
$userdob=(int)$query['dob'];
$data['dob'] = strftime('%Y', $now)- strftime('%Y',$userdob);
It is probably because $query['dob'] contains something other than an integer.
strftime() expects it's second argument to be a timestamp (that is, a unix-timestamp, an integer representing seconds past the epoch). If I had to guess, I'd guess that you're $query['dob'] contains some kind of string.

Trouble using MSSQL datetime in PHP (via SQLSRV)

I am going round in circles with this one! I'm doing the following:
Retrieving a date from an MSSQL datetime field via SQL/PHP
Sending the date to a new PHP page via the querystring
Trying to use that date in a new SQL query
I'm hitting problems here.
If I use echo:
echo $_REQUEST['rSessionDate'];
output: 15/10/2012
Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.
If I use date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
If I use strtotime():
echo strtotime($_REQUEST['rSessionDate']);
output: (nothing)
If I use date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
If I use date() with strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
I'm sure I'm totally missing something simple.
EDIT: I've tried a few new functions I found:
$rSessionDate = new DateTime($_REQUEST['rSessionDate']);
echo $rSessionDate->format('Y-m-d H:i:s');
output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
and:
$rSessionDate = date_create($_REQUEST['rSessionDate']);
echo date_format($rSessionDate, 'Y-m-d H:i:s');
output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
EDIT 2:
I have tried using CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
These might help to shed some light on what you're looking for.
http://www.ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime() is returning an epoch timestamp in your example above.
or check CAST and CONVERT (refers to MSSQL2000 but may still help you)
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx
if the date was retrieved from an MSSQL table and you want to use strtotime() in PHP and also don't want to change the date format to yyyy-mm-dd then you can use
CONVERT(VARCHAR(30), DateFromMSSQL, 121) as DateFromMSSQL

Error when trying to create an object DateTime in PHP

Today I got a project written in PHP. There's an error when I try to show a date in a table data set. This is the statement that generates the error:
$data = new DateTime($registro["previsao de entrega"];
The error message is this:
Fatal error: Uncaught exception 'Exception' with message
'DateTime::__construct() Failed to parse time string (2 dez 2011
16:00) at position 0 (2): Unexpected character' in
C:\www\fluxo_producao\Telas\TelaFluxoProducao.php:941 Stack trace: #0
C:\www\fluxo_producao\Telas\TelaFluxoProducao.php(941):
DateTime->__construct('2 dez 2011 16:0...') #1 {main} thrown in
C:\www\fluxo_producao\Telas\TelaFluxoProducao.php on line 941
What I discovered by myself was that if I change the parameter manually to "12 Dec 2006" for example, the function works. But the date the variable is passing is "12 Dez 2006" (Brazilian format, by the way, I'm Brazilian ^_^), and i found too that the default timezone in the server is "America/Sao_Paulo"... What do I have to change in the function or parameters to make it convert the format specified?
It needs to have a closing curly bracket on the end?
$data = new DateTime($registro["previsao de entrega"]);
If this is just a typo it could be the value of $registro["previsao de entrega"] is incorrect, what is it?
You can see allowed value formats here: http://www.php.net/manual/en/datetime.formats.date.php
It will only accept English month values, eg, Dec will work but Dez will not.
Verify that you have set your server to the correct time locale:
setlocale(LC_TIME, 'pt_BR');
This is required for strftime() and AFAIK all the date/time related functions and the DateTime class use the same library which will require/respect this setting.

Categories