skip to Main Content

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

2

Answers


  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

    Login or Signup to reply.
  2. 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');.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search