コード例 #1
0
ファイル: tool.php プロジェクト: RenzcPHP/3dproduct
 public static function get_date_byweek($week, $type = 1, $reftime = NULL)
 {
     if ($type == 1) {
         $weeknum = tool::get_week_en($week);
         if (!empty($reftime)) {
             $max = 5;
             $getdate = '';
             for ($i = 0; $i <= $max; $i++) {
                 $timeadd = strtotime($reftime) + $i * 86400;
                 $timedef = strtotime($reftime) - $i * 86400;
                 if (date('D', $timeadd) == $weeknum) {
                     $getdate = $timeadd;
                     break;
                 } elseif (date('D', $timedef) == $weeknum) {
                     $getdate = $timedef;
                     break;
                 }
             }
             if (!empty($getdate)) {
                 return date("Y-m-d", $getdate);
             } else {
                 return date("Y-m-d", time());
             }
         }
         if ($weeknum == '') {
             return date("Y-m-d H:i:s", time());
         } else {
             return date("Y-m-d H:i:s", strtotime($weeknum));
         }
     } elseif ($type == 2) {
         $weektype = array('Mon' => '1', 'Tue' => '2', 'Wed' => '3', 'Thu' => '4', 'Fri' => '5', 'Sat' => '6', 'Sun' => '7');
         $weeknum = '';
         foreach ($weektypek as $key => $value) {
             $check = strpos($week, $value);
             if ($check !== FALSE) {
                 $weeknum = $key;
                 break;
             }
         }
         if (!empty($reftime)) {
             $max = 5;
             $getdate = '';
             for ($i = 0; $i <= $max; $i++) {
                 $timeadd = strtotime($reftime) + $i * 86400;
                 $timedef = strtotime($reftime) - $i * 86400;
                 if (date('D', $timeadd) == $weeknum) {
                     $getdate = $timeadd;
                     break;
                 } elseif (date('D', $timedef) == $weeknum) {
                     $getdate = $timeadd;
                     break;
                 }
             }
             if (!empty($getdate)) {
                 return date("Y-m-d", $getdate);
             } else {
             }
         }
         if ($weeknum == '') {
             return date("Y-m-d H:i:s", time());
         }
         //当参考时间不能为空(参考时间前后5日内是否有相应日期)
         if (!empty($reftime)) {
             $max = 5;
             $getdate = '';
             for ($i = 0; $i <= $max; $i++) {
                 $timeadd = strtotime($reftime) + $i * 86400;
                 $timedef = strtotime($reftime) - $i * 86400;
                 if (date('D', $timeadd) == $weeknum) {
                     $getdate = $timeadd;
                     break;
                 } elseif (date('D', $timedef) == $weeknum) {
                     $getdate = $timeadd;
                     break;
                 }
             }
         } else {
             return date("Y-m-d H:i:s", strtotime($weeknum));
         }
         if ($weeknum == '') {
             return date("Y-m-d H:i:s", time());
         } else {
             return date("Y-m-d H:i:s", strtotime($weeknum));
         }
     }
 }