chormeで確認するとエラーが出ませんが、それ以外のブラウザで確認するとエラーが表示されています
Ajaxで月日を読み、phpでカレンダーを作成しています。
chromeではエラーが出ていないのですが、それ以外のブラウザでは以下のエラーが出ています。
Fatal error: Uncaught TypeError: DateTime::__construct() expects parameter 1 to be string, array given in C:\xampp\htdocs****.php on line 21
該当する箇所のソースは以下です。
php
1function funcIsAjax(){ 2 if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 3 return true; 4 return false; 5} 6 7if(funcIsAjax()) header("Content-type: text/plain; charset=UTF-8"); 8else{ 9 header("Location: {$base_url}"); exit; 10} 11 12date_default_timezone_set('Asia/Tokyo'); 13 14$today = $_POST; 15extract($today); 16 17$thisMonth = new DateTime($today); //21行目はここです。 18$dt = clone $thisMonth; 19$prev = $dt->modify('-1 month')->format('Y-m'); 20$dt = clone $thisMonth; 21$next = $dt->modify('+1 month')->format('Y-m'); 22 23$yearMonth = $thisMonth->format('Y F'); 24$month = $thisMonth->format('Y-m'); 25 26// カレンダー表示 27// 前月表示 28$tr = '<div class="cal_tr">'; 29$tail = null; 30//前月の最終日を習得 31$lastDayOfPrevMonth = new DateTime('last day of ' . $yearMonth . ' -1 month'); 32while ($lastDayOfPrevMonth->format('N') < 7) { 33 $tail = sprintf('<div></div>', $lastDayOfPrevMonth->format('d')) . $tail; 34 $tail = sprintf('<div class="cal cal_td empty"><div class="cal_body"></div></div>', $lastDayOfPrevMonth->format('d')) . $tail; 35 $lastDayOfPrevMonth->sub(new DateInterval('P1D')); 36 $prevmanth = $lastDayOfPrevMonth->format('Y-m'); 37} 38 39// 当月表示 40$body = null; 41$period = new DatePeriod ( 42 new DateTime('first day of' . $yearMonth), 43 new DateInterval('P1D'), 44 new DateTime('first day of' . $yearMonth . '+1 month') 45); 46 47$today = new DateTime('today'); 48foreach ($period as $day) { 49 if ($day->format('N') === '1'){ 50 $body .= '</div></div>'; 51 $body .= '<div class="cal_tr">'; 52 } 53 $days = $day->format('Y-m-d'); 54 55 $sch = ''; 56 foreach($holiday as $key) { 57 $id = $key['id']; 58 $user = $key['account_id']; 59 $from = $key['datetime_from']; 60 $to = $key['datetime_to']; 61 $type = $key['holiday_type']; 62 $comment = $key['comment']; 63 64 if($from == $days){ 65 $todayClass = ($day->format('Y-m-d') === $today->format('Y-m-d')) ? 'today' : ''; 66 $sch .= '<div class="area_btn" data-user="' . $user . '" data-from="' . $from . '" data-to="' . $to . '" data-type="' . $type . '" draggable="true">' . $type . '/' . ' ' .$user .'</div>'; 67 } 68 } 69 70 if($sch !== '') { 71 $todayClass = ($day->format('Y-m-d') === $today->format('Y-m-d')) ? 'today' : ''; 72 $body .= sprintf('<div class="cal cal_td" data-date="' . $days . '"><div class="cal_date day_%d %s">%d</div><div class="cal_plan"></div><div class="cal_body day">' . $sch . '</div></div>', $day->format('w'), $todayClass, $day->format('d')); 73 74 } else { 75 $todayClass = ($day->format('Y-m-d') === $today->format('Y-m-d')) ? 'today' : ''; 76 $body .= sprintf('<div class="cal cal_td" data-date="' . $days . '"><div class="cal_date day_%d %s">%d</div><div class="cal_plan"></div><div class="cal_body day"></div></div>', $day->format('w'), $todayClass, $day->format('d')); 77 } 78} 79 80// 次月表示 81$head = null; 82$firstDayOfNextMonth = new DateTime('first day of' . $yearMonth . '+1 month'); 83// var_dump($firstDayOfNextMonth->format('N')); 84while($firstDayOfNextMonth->format('N') > 1) { 85 $head .= '<div class="cal cal_td empty">' . $firstDayOfNextMonth->format('d'); 86 $head .= '<div class="cal_body"></div>'; 87 $head .= '</div>'; 88 89 $firstDayOfNextMonth->add(new DateInterval('P1D')); 90 $nextMonth = $firstDayOfNextMonth->format('Y-m'); 91} 92 93echo $tr . $tail . $body . $head; 94
jquery
1$(window).on('load', function(){ 2 $.ajaxSetup({ 3 type:'POST', 4 ifModified:false, 5 cache:false, 6 }); 7 8 var ajax_url = './calendar.php'; 9 10 var today = $('.thismonth').attr('data-month'); 11 var date = today; 12 console.log(date); 13 var data = { 14 today : date, 15 }; 16 17 return $.ajax({ 18 url:ajax_url, 19 data:data, 20 }).done(function(data, textStatus, jqXHR){ 21 $('.inner').html(data); 22 }).fail(function(jqXHR, textStatus, errorThrown ){ 23 $('.inner').children('.cal_tr').html(textStatus); 24 }); 25
どの様に対処すればよいか困っています。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー