2016年9月6日 星期二

預設日期+2日 (避開六日) (PHP & DB 作法)


需求: 預設日期為「核准日」+2 (避開週六日) , 這邊提供兩種做法參考: 

PHP Solution1:
// Reference PHP Official Site URL: http://php.net/manual/en/function.date.php 
// date function format about the N , 1 (for Monday) through 7 (for Sunday)
// This function will add working day to a given timestamp
function addworkinday($timestamp,$daystoadd){
    
     
$dayoftheweek date("N",$timestamp);
     
$sum =$dayoftheweek +$daystoadd;
    
while (
$sum >= 6) {
    
     
$daystoadd=$daystoadd+1;
    
$sum=$sum-1;
}
return 
$timestamp +(60*60*24*$daystoadd);

}
?>


My SQL DB Solution1:
Describe: 
        Try to use the WEEKDAY() funtion
        Return the weekday index for date  (0 = Monday, 1 = Tuesday, … 6 = Sunday)

直接查閱出特定日期的 day of week ex: 

Mode
First day of week
Range
Week 1 is the first week …
0
Sunday
0-53
with a Sunday in this year
1
Monday
0-53
with 4 or more days this year
2
Sunday
1-53
with a Sunday in this year
3
Monday
1-53
with 4 or more days this year
4
Sunday
0-53
with 4 or more days this year
5
Monday
0-53
with a Monday in this year
6
Sunday
1-53
with 4 or more days this year
7
Monday
1-53
with a Monday in this year

Example: 
mysql> SELECT WEEK('2008-02-20');
        -> 7
mysql> SELECT WEEK('2008-02-20',0);
        -> 7
mysql> SELECT WEEK('2008-02-20',1);
        -> 8
mysql> SELECT WEEK('2008-12-31',1);
        -> 53



Best Regards & thanks. 
George Huang 烤蕃薯 2016

沒有留言: