0

有没有办法使用 Carbon https://github.com/briannesbitt/Carbon制作一个简单的日历?

我是约会的新手,不太确定该怎么做。使用 PHP 日历类会更容易吗?我正在使用 Laravel 4 来实现这一点,所以希望使用 Carbon 是一种简单的方法。

4

3 回答 3

12

是的,你可以,操纵 Carbon 来创建一个月的简单 HTML 显示非常简单。

    $today = Carbon::today();

    echo '<h1 class="w3-text-teal"><center>' . $today->format('F Y') . '</center></h1>';

    $tempDate = Carbon::createFromDate($today->year, $today->month, 1);



    echo '<table border="1" class = "w3-table w3-boarder w3-striped">
           <thead><tr class="w3-theme">
           <th>Sun</th>
           <th>Mon</th>
           <th>Tue</th>
           <th>Wed</th>
           <th>Thu</th>
           <th>Fri</th>
           <th>Sat</th>
           </tr></thead>';

    $skip = $tempDate->dayOfWeek;


    for($i = 0; $i < $skip; $i++)
    {
        $tempDate->subDay();
    }


    //loops through month
    do
    {
        echo '<tr>';
        //loops through each week
        for($i=0; $i < 7; $i++)
        {
            echo '<td><span class="date">';

            echo $tempDate->day;

            echo '</span></td>';

            $tempDate->addDay();
        }
        echo '</tr>';

    }while($tempDate->month == $today->month);

    echo '</table>';

输出将如下所示: Html 日历

于 2017-03-10T01:01:28.783 回答
0

To me, Carbon is about parsing and displaying dates...it's an extension of the DateTime class. To create a calendar, you would still need to roll your own methods that build out the calendar. No need to reinvent the wheel as there are plenty of simple calendar scripts floating around the Internet. Depending on how it fits in with your project, you could go with the earlier suggestion of including a Laravel 4 Calendar package...or, you could go a different route altogether.

I would suggest going client-side for the calendar creation part, and have your PHP serve up the event data through Ajax requests. Adam Shaw has a pretty nice jQuery plugin called FullCalendar found here: http://arshaw.com/fullcalendar/ or https://github.com/arshaw/fullcalendar

I would definitely recommend looking at the demos. Dropping FullCalendar into your Laravel 4 project is no different than adding it into any other project. Load jQuery, and then load FullCalendar. Check out the basic usage page: http://arshaw.com/fullcalendar/docs/usage/

Just thought I would throw this out there as one more option.

于 2013-09-20T03:50:31.340 回答
0

正如人们在评论中所说:有点模糊,但我假设你需要创建一个日历并用它做你的事情。

是的,你可以使用 Carbon 来帮助你管理日期和时间,但你最好不要从头开始,你可以通过使用(如果可能的话,帮助增长)一个 Laravel 4 日历包来做到这一点:)

https://github.com/Crinsane/LaravelCalendar

于 2013-09-20T03:13:05.673 回答