【问题标题】:Fill the calendar with unix time in php在php中用unix时间填充日历
【发布时间】:2015-11-19 14:33:13
【问题描述】:

我正在创建一个每周工作的表格日历。日历是这样的

我使用找到的代码here 来设置我的标题日期。我的代码如下所示:

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

现在,我需要以Unix 格式输入我的data-reservation_time 日期(这就是为什么有$current_time 变量),该单元格。因此,例如,在 20 日星期五 8:00 的单元格中,该单元格中应该有 1448006400。稍后我将使用它来存储在数据库中。

但是我该怎么做呢?我被困在这一点上。任何帮助表示赞赏。

编辑

找到了答案。它在下面:)

【问题讨论】:

    标签: php unix calendar


    【解决方案1】:

    您可以使用 strtotime 获取 unixtimstamp:

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

    【讨论】:

    • 我知道我可以,但我需要在表格中为每个单元格显示日期和时间。
    【解决方案2】:

    类似:

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

    为了获取时间戳,请从您的 $dt 对象中获取日期,并从循环中获取时间。

    【讨论】:

    • 这将返回下周而不是上表中的一周,我已经尝试过了:\
    【解决方案3】:

    好的,所以答案比我想象的要简单。好吧,我仍然不能 100% 确定第一部分是绝对正确的,但它应该是(按照我的逻辑)。

    首先,创建一个本周的日期数组(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);
    }
    

    第一个if else 循环是检查今天是否是星期一,以便您在下一个星期日(同一周)填充数组。如果是星期二,则时间戳参考是最后一个星期一。

    然后只需执行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>';
    

    我的时间日期在data-time,适合 jquery 抓取:D

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多