Boostrap年历yii2扩展 - 月解析问题

时间:2018-01-28 16:31:09

标签: javascript php yii2 yii-extensions

我已将bootstrap year calendar yii2整合到我的网站中。我使用活动数据提供程序加载日历项目。到目前为止我遇到过一个问题。每个日历项目的月份不正确。每个日历项目添加一个月。例如5月19日的项目显示为6月19日等。过去有没有人遇到过类似的问题?

print screen

echo ActiveCalendar::widget([
'language' => 'es',

'dataProvider' => new ActiveDataProvider([
    'query' => Event::find()->andWhere(['calendar_id' => $calendar->id])
]),
'options' => [
    // HTML attributes for the container.
    // the `tag` option is specially handled as the HTML tag name
],
'clientOptions' => [
    'enableContextMenu' => true,
    'enableRangeSelection' => true,
    'displayWeekNumber' => false,
    'alwaysHalfDay' =>true,
    'disabledDays'=> [],
    'startYear'=> '2018',
    'minDate'=> new JsExpression('new Date("2018-01-01")'),
    'maxDate'=> new JsExpression('new Date("2018-12-31")'),

    // JS Options to be passed to the `calendar()` plugin.
    // see http://bootstrap-year-calendar.com/#Documentation/Options
    // The `dataSource` property will be overwritten by the dataProvider.
],
'clientEvents' => [
    'mouseOnDay' => '',
    // JS Events for the `calendar()` plugin.
    // see http://bootstrap-year-calendar.com/#Documentation/Events
]
]);

$i = 1;
foreach($calendar->events as $event) {
echo 'Event ' . $i . ' start date: ' . $event->startDate . '- ';
echo 'end date: ' . $event->startDate . '<br/>';
$i++;
}

编辑:插件文档中的模型

class Event extends \yii\db\ActiveRecord implements DataItem
{

public function getName()
{
    return $this->event_title;
}

public function getStartDate()
{
    return JsExpressionHelper::parse($this->event_date);
}

public function getEndDate()
{
    return JsExpressionHelper::parse($this->event_date_end);
}

http://bootstrap-year-calendar.com/

1 个答案:

答案 0 :(得分:0)

Helper JsExpressionHelper中存在逻辑错误,需要修复方法parsePhpDate

What's The problem

当你调用方法时,它正在做什么

return JsExpressionHelper::parse($this->event_date);

在你的模型中传递日期字符串或它返回的对象Javascript表达式new Date(2018, 05, 19)现在仔细观察,当你调用Date javascript函数时会有所不同通过将日期作为Date(string)并将日期作为Date(year,month,date)传递。

与javascript Date()函数一样,起始月January0而不是1

参见示例

&#13;
&#13;
console.log("new Date(2015,10,10) outputs : ", new Date(2015, 10, 10));
console.log("new Date('2015-10-10') outputs : ", new Date('2015-10-10'));
&#13;
&#13;
&#13;

所以在这种情况下,助手通过使用像JsExpression('new Date(' . $date->format('Y, m, d') . ')');之类的php日期函数传递月份日期和年份 而且,如果没有减1,则意味着它使用的是php日期函数,它具有从1-1201-12开始的月份表示。 因此,datepicker正在将May日期加载到June,这在技术上是正确的。

What to do

您需要更改函数返回的javascript表达式,或者使用string返回表达式,或者手动提供所有参数。

但是由于帮助器具有所有静态功能而您无法扩展它们,我建议

1.将该助手复制到common/components文件夹,并将其重命名为ExperssionHelper

2.在您的模型中,将对JsExpressionHelper的调用更改为ExpressionHelper

3.然后将代码更改为以下内容,您可以使用2种方法首先使用字符串格式尝试此方法(如果有效)

 /**
     * Parses a DateTime object to a `JsExpression` containing a javascript date
     * object.
     *
     * @param DateTime $date
     * @return JsExpression
     */
    public static function parsePhpDate(DateTime $date)
    {
        return new JsExpression('new Date("' . $date->format('Y-m-d') . '")');
    }

如果出于某种原因,日历无法使用该格式,请将其更改为以下内容并将起作用

/**
 * Parses a DateTime object to a `JsExpression` containing a javascript date
 * object.
 *
 * @param DateTime $date
 * @return JsExpression
 */
public static function parsePhpDate(DateTime $date)
{
    $month=$date->format('m')-1;
    $day=$date->format('d');
    $year=$date->format('Y');

    return new JsExpression('new Date('.$year.','.$month.','.$day.')');

}

您可以将以下内容复制到文件夹common/components

<?php 
namespace common\components;

use DateTime;
use yii\base\InvalidParamException;
use yii\web\JsExpression;

/**
 * Helper to parse data into
 * [JsExpression](http://www.yiiframework.com/doc-2.0/yii-web-jsexpression.html) * which is what the widgets expect when dealing with dates for the JS plugin.
 *
 * Its main usage is in classes implementing the [[DataItem]] interface
 *
 * ```php
 * public function getStartDate()
 * {
 *     return JsExpressionHelper::parse($this->start_date);
 * }
 * ```
 *
 * @author Angel (Faryshta) Guevara <angeldelcaos@gmail.com>
 */
class ExpressionHelper
{
    /**
     * Parses a date to a `JsExpression` containing a javascript date object.
     *
     * @param DateTime|string|integer $date
     * @param string $format only used when the ``$date` param is an string
     * @return JsExpression
     */
    public static function parse($date, $format = 'Y-m-d')
    {
        if (is_string($date)) {
            echo "string<br />";
            return self::parseString($date, $format);
        }
        if (is_integer($date)) {
            return self::parseTimestamp($date);
        }
        if (is_object($date) && $date instanceof DateTime) {
            return self::parsePhpDate($date);
        }

        throw new InvalidParamException('The parameter `$date` must be a '
            . 'formatted string, a timestamp or a `DateTime` object'
        );
    }

    /**
     * Parses a DateTime object to a `JsExpression` containing a javascript date
     * object.
     *
     * @param DateTime $date
     * @return JsExpression
     */
    public static function parsePhpDate(DateTime $date)
    {
        $month=$date->format('m')-1;
        $day=$date->format('d');
        $year=$date->format('Y');

        return new JsExpression('new Date('.$year.','.$month.','.$day.')');
    }

    /**
     * Parses an string to a `JsExpression` containing a javascript date object.
     *
     * @param string $date
     * @param string $format used to create a temporal `DateTime` object
     * @return JsExpression
     * @see http://php.net/manual/es/datetime.format.php
     */
    public static function parseString($date, $format = 'Y-m-d')
    {
        return self::parsePhpDate(DateTime::createFromFormat(
            $format,
            $date
        ));
    }

    /**
     * Parses a timestamp integer to a `JsExpression` containing a javascript
     * date object.
     *
     * @param integer $date
     * @return JsExpression
     * @see http://php.net/manual/es/datetime.settimestamp.php
     */
    public static function parseTimestamp($date)
    {
        $PhpDate = new DateTime();
        $PhpDate->setTimestamp($date);
        return self::parsePhpDate($PhpDate);
    }
}

您的Event模型应该是

use common\components\ExpressionHelper;
class Event extends \yii\db\ActiveRecord implements DataItem
{

public function getName()
{
    return $this->event_title;
}

public function getStartDate()
{
    return ExpressionHelper::parse($this->event_date);
}

public function getEndDate()
{
    return ExpressionHelper::parse($this->event_date_end);
}