打印出时间表

时间:2017-07-28 17:23:35

标签: javascript php mysql

我试图更好地学习php,html,ajax和javascript以便在编程方面做得更好。但是,我一直坚持试图打印出一周中的某一天。我想我可以使用date_time并从mysql打印出日期和星期,然后将其打印到屏幕上,但我遇到了麻烦。到目前为止,我已经查找了某些功能,如

week()

date(curdate() - interval weekday(curdate()) day)

来自其他线程,但我仍然无法从周日开始一周,然后自动打印出那一天和其余的一周。任何人都可以帮助或至少键入我需要这样做的功能?如果它有帮助,那么目标就是打印出来:

Sunday 23|monday 24|tuesday 25| ETC

编辑: 我想明确表示我不是在寻找设计或任何东西。我主要是试图进入一天,比如某个星期的星期日,然后打印整个星期

3 个答案:

答案 0 :(得分:0)

我认为你的问题会被投票,但我建议你阅读:

https://msdn.microsoft.com/en-us/library/ee634550.aspx

您可以在工作日()函数中添加1参数,以便在星期日开始您的一周。

希望有所帮助。

答案 1 :(得分:0)

我建议你看一下DateTime对象。

你还应该创建一个这样的函数:

function printWeek($date) {
    // Start from the given date
    $currentDay = DateTime::createFromFormat("Y-m-d", $date);   
    $currentWeekDay = $currentDay->format("w");

    // Get the first week day
    $day = $currentDay->sub(new DateInterval("P{$currentWeekDay}D"));
    $weekDays = [];

    // Calculate all week days and store it
    do {
        $weekDays[] = $day->format("l d");      
        $day->add(new DateInterval("P1D"));
    } while($day->format("w") != 0);

    // Print all weekdays
    echo implode($weekDays, " | ");
}

输出将如下:

printWeek("2017-07-22");

// Sunday 16 | Monday 17 | Tuesday 18 | Wednesday 19 | Thursday 20 | Friday 21 | Saturday 22

答案 2 :(得分:0)

可能有更好的方法,但这是我的代码:

$cur_month = date ( 'm' );
$cur_year = date ( 'Y' );
$days_month = cal_days_in_month ( CAL_GREGORIAN, $cur_month, $cur_year ); // Calculate the number of days in the month

// Get month name from number
$cur_dateObj = DateTime::createFromFormat ( '!m', $cur_month );
$cur_month_name = $cur_dateObj->format ( 'F' );

$day_count = 1;
while ( $day_count <= $days_month )
{
    $day_count = sprintf ( '%02d', $day_count );
    $full_date = "" . $cur_year . "-" . $cur_month . "-" . $day_count . "";
    $dayname = date ( 'D', strtotime ( $full_date ) );
    echo $dayname . " " . $day_count . "|";
    $day_count++;
}

我正在使用稍微修改过的版本来创建计划表。

enter image description here

如果您想了解更多信息,请查看以下内容:http://php.net/manual/en/function.date.php

相关问题