Search code examples
phpunix-timestamp

PHP: How to determine Unix Timestamp of midnight tomorrow?


I have the following code to determine the unix timestamp of midnight tomorrow. It seems pretty hacky to me and I wondered if anyone had a more elegant solution.

date("U", strtotime(date("Y-m-d 00:00:00", strtotime("tomorrow"))));

Solution

  • This should be enough:

    <?php
    $t = strtotime('tomorrow');
    

    You can also set a time:

    <?php
    $t = strtotime('tomorrow 14:55');
    

    Please note that, as other date features in PHP, it will use the default time zone unless asked otherwise:

    date_default_timezone_set('Asia/Tokyo');
    $t = strtotime('tomorrow');
    var_dump($t, date('r', $t));
    
    date_default_timezone_set('Europe/Madrid');
    $t = strtotime('tomorrow');
    var_dump($t, date('r', $t));
    
    date_default_timezone_set('Europe/Madrid');
    // Generate midnight in New York, display equivalent Madrid time
    $t = strtotime('tomorrow America/New_York');
    var_dump($t, date('r', $t));
    
    int(1502204400)
    string(31) "Wed, 09 Aug 2017 00:00:00 +0900"
    int(1502229600)
    string(31) "Wed, 09 Aug 2017 00:00:00 +0200"
    int(1502251200)
    string(31) "Wed, 09 Aug 2017 06:00:00 +0200"