laravel 使用 Carbon 的简单日历
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18908000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Calendar using Carbon
提问by Lynx
Is there a way to make a simple Calendar using Carbon https://github.com/briannesbitt/Carbon?
有没有办法使用 Carbon https://github.com/briannesbitt/Carbon制作一个简单的日历?
I am new with dates and not really sure how to go about it. Would it be easier to use the PHP calendar class? I am using Laravel 4 to accomplish this so was hoping using Carbon would be easy way to make one.
我是约会的新手,不太确定如何去做。使用PHP日历类会更容易吗?我正在使用 Laravel 4 来实现这一点,所以希望使用 Carbon 是一种简单的方法。
回答by jrtechs
Yes you can, it is quite simple to manipulate Carbon to create a simple HTML display of a month.
是的,您可以,操作 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>';
The output will look something like this:
输出将如下所示:
回答by Antonio Carlos Ribeiro
As people said in comments: a little vague, but I'll assume you need to create a calendar and do your things with it.
正如人们在评论中所说:有点模糊,但我假设您需要创建一个日历并用它来做您的事情。
Yes you can use Carbon to help you manage date and time, but you better not start this from scratch, you can do it by using (and helping to grow, if possible) a Laravel 4 Calendar Package :)
是的,你可以使用 Carbon 来帮助你管理日期和时间,但你最好不要从头开始,你可以通过使用(并帮助成长,如果可能的话)一个 Laravel 4 日历包:)
回答by fletch
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.
对我来说,Carbon 是关于解析和显示日期......它是 DateTime 类的扩展。要创建日历,您仍然需要推出自己的方法来构建日历。无需重新发明轮子,因为 Internet 上有很多简单的日历脚本。根据它如何适合您的项目,您可以采用早先的建议,即包含 Laravel 4 日历包……或者,您可以完全走不同的路线。
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
我建议在客户端进行日历创建部分,并让您的 PHP 通过 Ajax 请求提供事件数据。Adam Shaw 有一个非常不错的 jQuery 插件,叫做 FullCalendar,可以在这里找到:http://arshaw.com/fullcalendar/ 或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/
我绝对会建议您查看演示。将 FullCalendar 放入 Laravel 4 项目与将其添加到任何其他项目没有什么不同。加载 jQuery,然后加载 FullCalendar。查看基本使用页面:http: //arshaw.com/fullcalendar/docs/usage/
Just thought I would throw this out there as one more option.
只是想我会把它作为另一种选择。