Fill calendar with unix time in php

I create a table calendar that runs weekly. The calendar is as follows:

enter image description here

I used the code I found here to adjust the dates of my header. The code I have is as follows:

$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 specify the data-reservation_time date in Unix format (which is the reason for the $current_time variable) of this cell. So, for example, in a cell on Friday 20 at 8:00 this cell should have 1448006400 . I will use this later for storage in the database.

But how do I do this? I am stuck on this issue. Any help is appreciated.

EDIT

Found the answer. This is below :)

+5
source share
3 answers

So the answer was simpler than I thought. Well, I'm still not 100% sure that the first part is absolutely correct, but it should be (according to my logic).

First create an array of dates this week (given 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 Monday is set today so that you fill the array next Sunday (in the same week). If it's Tuesday, then the timestamp reference is the last Monday.

And then just do 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 over time are in data-time , mature to capture jquery: D

0
source

You can get unixtimstamp with strtotime:

 <?php $date = date("d MY"); $time = "15:00"; $unixTime = strtotime($date . " " . $time); ?> 
0
source

Sort of:

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

To get the timestamp, get the day from your $dt object and the time from the loop.

0
source

Source: https://habr.com/ru/post/1236340/


All Articles