5

I'm creating a table calendar, that works on the weekly basis. The calendar looks like this

enter image description here

I used code I found here to set up my header dates. The code I have looks like this:

$dt = new DateTime;
$dt->setISODate($dt->format('o'), $dt->format('W'));
$year = $dt->format('o');
$week = $dt->format('W');
$current_time = time(); 

$return .= '<table class="reservation_time_table">
                <tr><th>'.esc_html__('Hours', 'time_reservation').'</th>';
do {
    $return .= '<th>' . $dt->format('l') . '<br>' . $dt->format('d M Y') . '</th>';
    $dt->modify('+1 day');
} while ($week == $dt->format('W'));
$return .= '</tr>';
for ($hour=8; $hour < 23 ; $hour++) {
    $return .= '<tr>';
        $return .= '<th>'.$hour.':00</th>';
        for ($i=0; $i <7 ; $i++) {
            $return .= '<td data-reservation_time=""></td>';
        }
    $return .= '</tr>';
}

$return .= '</table>';

Now, I need to put in my data-reservation_time the date in Unix format (that's why there is $current_time variable), of that cell. So for instance in the cell for Friday 20 in 8:00 there should be 1448006400 in that cell. I'll use this later on to store in the database.

But how do I do that? I'm stuck at this point. Any help is appreciated.

EDIT

Found the answer. It's below :)

Community
  • 1
  • 1
dingo_d
  • 11,160
  • 11
  • 73
  • 132

3 Answers3

0

You can get the unixtimstamp using strtotime:

<?php
$date = date("d M Y");
$time = "15:00";
$unixTime = strtotime($date . " " . $time);
?>
Liam Wiltshire
  • 1,254
  • 13
  • 26
0

Something like:

$return .= '<td data-reservation_time="' . strtotime($dt->format("Y-m-d") . " " . $hour . ":00:00") . '"></td>';

In order to get the timestamp, get the day from your $dt object, and time from the loop.

Oliver Maksimovic
  • 3,204
  • 3
  • 28
  • 44
0

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

Community
  • 1
  • 1
dingo_d
  • 11,160
  • 11
  • 73
  • 132