Fill the calendar with unix time in php
Ok, so the answer was simpler than I thought. Well I am still not 100% sure that the first part is absolutely correct, but it should be (by my logic).
First, create an array of dates for this week (curtesy of this answer)
$today = time();if (date('D', $today) === 'Mon') { $timestamp = strtotime('this Monday');} else{ $timestamp = strtotime('last Monday');}$days = array();$dates = array();for ($i = 0; $i < 7; $i++) { $days[] = strftime('%A <br /> %d %b %Y', $timestamp); $dates[] = strftime('%d %b %Y', $timestamp); $timestamp = strtotime('+1 day', $timestamp);}
The first if else
loop was to check if today is Monday, so that you fill the array towards the next Sunday (in the same week). If it's Tuesday, then the timestamp reference is the last Monday.
And then just do a foreach
$return .= '<table class="reservation_time_table"> <tr><th>'.esc_html__('Hours', 'time_reservation').'</th>'; foreach ($days as $day => $day_value) { $return .= '<th>'.$day_value.'</th>'; } $return .= '</tr>'; for ($hour=8; $hour < 23 ; $hour++) { $return .= '<tr>'; $return .= '<th>'.$hour.':00</th>'; foreach ($dates as $date => $date_value) { $full_hour = $hour. ':00'; $return .= '<td data-time="'.strtotime($date_value. ' ' .$full_hour).'"></td>'; } $return .= '</tr>'; }$return .= '</table>';
And my dates with times are in data-time
, ripe for jquery grabbing :D