-1

I need to get the next 0900 date in PHP. For example:

  1. If it is 0859 right now, the next 0900 is today at 0900
  2. If it is 0901 right now, the next 0900 is tomorrow at 0900

I'm only interested in the date in yyyymmdd format. Any suggestions on how to do this? I'm thinking that date and strtotime are the functions I need to use, but I'm just not sure about how to handle the "next 0900".

This doesn't work: echo date('Ymd', strtotime('next 0900'));

StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441

2 Answers2

3

This should get you the date of the next 9:00:

date('Ymd', strtotime('9:00'));

EDIT: No, actually that doesn't work. Try this instead (not quite as simple, but works):

// if the current time is greater than the time at 9:00, give 9:00 tomorrow
// Otherwise give 9:00 today
$nextnth = (mktime(9) < time()) ? strtotime('tomorrow 0900') : strtotime('today 0900');
echo date('Ymd', $nextnth);
Simon M
  • 2,931
  • 2
  • 22
  • 37
  • It's past `0900` where I am testing your code. The following returns the same date: `echo date('Ymd', strtotime('today'));` and `echo date('Ymd', strtotime('0900'));`, which is wrong. – StackOverflowNewbie May 26 '13 at 02:35
  • @StackOverflowNewbie Just figured out the same. Perhaps `date('Ymd', strtotime('+1 09:00'));` would work better? – Simon M May 26 '13 at 02:36
  • @StackOverflowNewbie See my edit... On a related note, that site is handy! It's always such a mess making a file in my local AMP stack, that will make it a lot easier to do random testing ;) – Simon M May 26 '13 at 02:55
0

Try this:

$x = strtotime('tomorrow 09:00') - strtotime("now");

if ($x > 86400)  // If it's more than 1 day we're actually before 09:00, subtract 1 day
    $x -= 86400;

echo "Now: ".date("Y-m-d H:i:s")."\r\n";
echo "Time until 0900: ".date("H\h i\m s\s", $x).".\r\n";

Alternatively:

$x = (strtotime('tomorrow 09:00') - strtotime("now")) % 86400;

echo "Now: ".date("Y-m-d H:i:s")."\r\n";
echo "Time until 0900: ".date("H\h i\m s\s", $x).".\r\n";
ccKep
  • 5,786
  • 19
  • 31
  • Again, never add/subtract 86400, as not all days are 24 hours long. This will produce unexpected results twice a year. – ItalyPaleAle Jun 05 '13 at 22:46