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
CodePudding user response:
Because of a php version change:
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
CodePudding user response:
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.
It's unclear what you're trying to do, but for an integer representing "now", use time()
; for an integer representing some other date, use something like strtotime('2020-01-01 19:45');
.