-1

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

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • 1
    Regardless of whether it used to work, the error message is pretty clear: you're passing a string, it expects an integer. The documentation for `mktime` is here: https://php.net/mktime Probably this code has been broken for some time, and you've moved to a newer version of PHP which is better at telling you. – IMSoP Feb 22 '22 at 10:46
  • Don't know how this could've worked unless you had an override for the function (see https://stackoverflow.com/questions/3927995/override-default-php-function), as per https://www.php.net/manual/en/function.mktime.php, this function expects multiple arguments, one for each of the time components, but you are passing it just one argument - one which is supposed to be a number (hour), but you are passing it a string. – jnko Feb 22 '22 at 10:49

2 Answers2

1

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

  • 1
    Just to be clear, the version change is that the error is more obvious; the code was always wrong. – IMSoP Feb 22 '22 at 10:55
1

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');.

IMSoP
  • 89,526
  • 13
  • 117
  • 169