2015-11-19 8 views
5

Sto creando un calendario da tavolo, che funziona su base settimanale. Il calendario si presenta cosìRiempi il calendario con unix time in php

enter image description here

Ho usato il codice che ho trovato here per impostare le mie date di intestazione. Il codice che ho è simile al seguente:

$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>'; 

Ora, ho bisogno di mettere nel mio data-reservation_time la data in formato Unix (è per questo che v'è $current_time variabile), di quella cella. Quindi, per esempio nella cella per venerdì 20 alle 8:00 ci dovrebbe essere 1448006400 in quella cella. Lo userò più tardi per memorizzarlo nel database.

Ma come faccio? Sono bloccato a questo punto. Qualsiasi aiuto è apprezzato.

EDIT

trovato la risposta. E 'sotto :)

risposta

0

Ok, quindi la risposta è stata più semplice di quanto pensassi. Beh, non sono ancora sicuro al 100% che la prima parte sia assolutamente corretta, ma dovrebbe essere (secondo la mia logica).

In primo luogo, creare una serie di date per questa settimana (curtesy di 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); 
} 

La prima if else ciclo è stato quello di verificare se oggi è Lunedi, in modo che si riempie la matrice verso la prossima Domenica (in la stessa settimana). Se è martedì, il riferimento timestamp è l'ultimo lunedì.

E poi basta fare un 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>'; 

E le mie date con tempi sono in data-time, maturi per jQuery afferrare: D

0

È possibile ottenere l'unixtimstamp utilizzando strtotime:

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

So che posso, ma ho bisogno di visualizzare la data e l'ora in la tabella per ogni cella. –

0

Qualcosa di simile:

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

Al fine di ottenere il timestamp, iniziare la giornata dal vostro $dt oggetto, e il tempo dal ciclo.

+0

Questo ritorna la settimana successiva anziché la settimana nella tabella sopra, l'ho già provato: \ –