Bootstrap 年历 yii2 扩展 - 月份解析问题

Boostrap year calendar yii2 extension - Month parsing issue

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

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/

Helper JsExpressionHelper 中似乎存在逻辑错误,需要在方法 parsePhpDate 中进行修复。

What's The problem

它的作用是当你调用方法时

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

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

如 javascript Date() 函数起始月份 January0 而不是 1

查看示例

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

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

What to do

您需要更改由函数 return 编辑的 javascript 表达式 return 带有字符串的表达式或手动提供所有参数。

但是由于助手具有所有静态函数并且您不能扩展它们我建议

1.Copy common/components 文件夹的助手并将其重命名为 ExperssionHelper.

2.In 您的模型将对 JsExpressionHelper 的调用更改为 ExpressionHelper

3.Then 将代码更改为以下你可以有 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);
}