Reputation: 91
I will like to get the next Thursday relative to a particular date if the date passed is not Thu. This is how far have gone with the code.
<?php
$string = '2014.07.16';
$date = DateTime::createFromFormat("Y.m.d", $string);
$result = $date->format("D");
if ($result != 'Thu')
{
$result2 = strtotime('next Thursday');
echo date('Y.m.d', $result2);
}
?>
In the above code, the variable $string is the date passed which is a 'Wed' I will like to get '2014.07.16' which is the next Thursday relative to '2014.07.17'. That means if $string is '2014.07.21' which is a Mon, the returned date will be '2014.07.24' that's the next Thursday relative to '2014.07.21' I am getting '2014.07.31' at the moment which is the next Thursday to today's date.
How do I get the next Thursday relative to $string (passed date)
Thank you.
Upvotes: 0
Views: 148
Reputation: 9635
try this
$string = '2014.07.16';
$date = DateTime::createFromFormat("Y.m.d", $string);
$result = $date->format("D");
if ($result != 'Thu')
{
$result2 = strtotime('next Thursday', strtotime($date->format("d-M-Y")));
echo date('Y.m.d', $result2);
}
OUPUT : 2014.07.17
Upvotes: 2
Reputation: 21
strtotime is pretty smart and can work with all kind of parameters. However you need to pass the date in a different format than you are using. Just replace . with - before passing the date to strtotime:
$string = '2014.07.16';
$string = str_replace('.', '-', $string);
if (date('w', strtotime($string)) != 4) {
$date = date('Y.m.d', strtotime($string . ' next thursday'));
echo $date;
}
date('w') returns the day of the week as integer. It's nicer to use this for comparison than a string.
Upvotes: 0