Black Friday sale is now on! 50% off Laravel: The Modular Way. Learn more

Quick way to add hours and minutes with PHP

David Carr

PHP & MySQL Tutorials

    The following is really easy way to add days, minutes, hours and seconds to a time using PHP. Using the date function to set the format of the date to be returned then using strtotime to add the increase or decrease of time then after a comma use another strtotime passing in the start date and time.

    //set timezone
    date_default_timezone_set('GMT');
    
    //set an date and time to work with
    $start = '2014-06-01 14:00:00';
    
    //display the converted time
    echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
    

    Times can be entered in a readable way:

    • +1 day = adds 1 day
    • +1 hour = adds 1 hour
    • +10 minutes = adds 10 minutes
    • +10 seconds = adds 10 seconds

    To sub-tract time its the same except a - is used instead of a +

    Laravel Modules Book by David Carr

    Help support the blog so that I can continue creating new content!

    Subscribe to my newsletter

    Subscribe and get my books and product announcements.

    © 2009 - 2022 DC Blog. All code MIT license. All rights reserved.