简体   繁体   中英

Unix timestamp of the next occurrence of 4pm, 5:20pm, etc

Any idea in PHP how to get a unix timestamp of the next occurring specified hour and minute?

Thank you

Calling strtotime("4pm") will give you the time for today at 4pm. If it's already past 4pm, you can just add 60*60*24 to the given timestamp

// Example blatantly copied from AndrewR, but it uses strtotime   
$nextfourpm = strtotime("4pm");    
if ($nextfourpm < time()) {
  $nextfourpm += 60*60*24;
}

You could do something like this.

$nextTime = mktime(16, 0, 0);
if($nextTime < time()){
    $nextTime += 86400;
}

Now is 15:20

You want tomorrow at 16:22

BEST way for you is to create function with posting hour+ and minutes+ you want to go beyond - pure difference in time). By doing this way PHP cannot make any mistakes. And PHP can make mistakes while working with dates and stuff.

//stuff to post..
$hour = 25;
$minute = 2;

           function makemydate($hour,$minute)
           {
              if($hour > 0) 
              { 
              $hour = ($hour*3600); 
              }

              if($minute > 0) 
              { 
              $minute = ($minute*60);
              }

            $add = ($hour+$minute);
            $now = time();

            $unixtimestamp = ($now+$add);
            return $unixtimestamp;
           }

    echo makemydate;

There you go its a unix timestamp.. You need to specify time whatsoever :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM