6

I'm looking for the next Thursday after a specific date, say 2014-02-25. The problem I'm having here is that when I use the below code, the time seems to be erased.

<?php
    $timestamp = '2014-02-25 10:30:00';

    echo date('Y-m-d H:i:s', strtotime("next Thursday", strtotime($timestamp)));
?>

The result I am getting is 2014-02-27 00:00:00 when I would like it to be 2014-02-27 10:30:00

Is there something I am missing here that is causing the time to be set to midnight?

I appreciate the help, thanks.

What have you tried
  • 11,018
  • 4
  • 31
  • 45

1 Answers1

9

There is no time format that can directly express this. You need to produce a format like

next Thursday 10:30:00

... manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:

$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);

echo date('Y-m-d H:i:s',
    strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);

The same results could be achieved using string concatenation:

echo date('Y-m-d', strtotime("next Thursday", $timestamp)
    . ' ' . date('H:i:s', $timestamp);

The documentation for so called relative time formats can be found here

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Hey, thanks for the answer. Testing this, I'm getting the same response as my original code which is midnight on the 27th. The result I'm looking for is 10:30 on the 27th. Any chance of that happening? – What have you tried Feb 26 '14 at 23:48
  • Ok, got it. :) .. Let me check – hek2mgl Feb 26 '14 at 23:49
  • Beautiful, thank you .. could you point me towards some documentation for the above? I've been reading about strtotime all day and haven't come across the concatenation as done above in your answer. I'm referring to the way you concatenated the time to "Next thursday" – What have you tried Feb 26 '14 at 23:58
  • 1
    Have added that. The term you are referring to is called *relative time formats* – hek2mgl Feb 27 '14 at 00:07