PHP Check Particular Day
I need to change shipping day to next Tuesday if the day is Friday.Here is my code.
$tomorrow = mktime(0,0,0,date("m"),date("d")+$tt+1,date("Y"));
$duedate = date("d - M D", $tomorrow);
$str = substr($duedate, 9, 11);
$fri='Fri';
$sat='Sat';
if(strcmp($str,$fri)==0)
{
$tomorrow = mktime(0,0,0,date("m"),date("d")+4,date("Y"));
$duedate = date(开发者_JAVA百科"d - M D", $tomorrow);
}
But i think the if loop is not checking friday correctly.So adding 4 to $tommorrow
is also not working.Any pointers?
This should be enough:
if (date('N') == 5) {
//set duedate to next Tuesday
$DateTime = new DateTime('now');
$DateTime->add(new DateInterval('P4D')); //add 4 days
$dueDate = $DateTime->format('your format');
}
N gives back an ISO standardized number which is 5 for friday. Date needs no second argument, in that case it takes the current timestamp.
See http://www.php.net/manual/en/book.datetime.php for more information on DateTime.
$tomorrow = mktime(0,0,0,date("m"),date("d")+$tt+1,date("Y"));
$duedate = date("d - M D", $tomorrow);
if (date("N", $tomorrow) == 5) {
$tomorrow = mktime(0,0,0,date("m"),date("d")+4,date("Y"));
$duedate = date("d - M D", $tomorrow);
}
Firstly try extracting the day just using the date function, for example:
date('D'); // Mon through Sun
or
date('N'); // 1 (for Monday) through 7 (for Sunday)
Secondly use the strtotime function to add days to a date, for example:
strtotime("+4 day",$date); // Add 4 days to date
$nextTuesday = strtotime('next tuesday');
精彩评论