Search code examples
phpdatetimetimetimestampdatediff

php adding 1 hour to the timestamp


I am trying to add 1 hour to a timestamp field fetched from database using the following code.

date($ls['created_at'], strtotime('+1 hour'));

However, this doesn't seem to work. It returns the same time as in database. Am I missing something? Or, is the code deprecated? What is the proper solution?


Solution

  • You need to give it the correct syntax to use this, You need to send the time to change with the change itself in the function - for example (using date for wanted format):

    $date = "22-02-2021 14:22:22";
    echo date("d-m-Y H:i:s", strtotime($date.' +1 hour'));
    

    This will return:

    22-02-2021 15:22:22
    

    Same as this:

    echo date("d-m-Y H:i:s", strtotime("22-02-2021 14:22:22 + 1 hour"));
    

    The idea is that you strtotime receives the date and data to change in one string like this :

    echo strtotime("22-02-2021 14:22:22 + 2 hour");
    

    Will return:

    1614010942
    

    Here I removed the Date Format so I received a unix timestamp format