Prettyscripts

PHP: List Months Between 2 Dates

Based on code:

function list_months($start, $end) {
    $period = new DatePeriod(
        (new DateTime($start))->modify('first day of this month'),
        DateInterval::createFromDateString('1 month'),
        (new DateTime($end))->modify('first day of next month')
    );
    $month = [];
    foreach ($period as $dt) {
        $month[] = $dt->format('Ym');
    }
    return $month;
}

Notes:

  • Line 3: get the first of the month from start date.
  • Line 5: get the first of the end date’s next month. This to include month of end date in result.

Yii2: Authorization without RBAC

While RBAC allows you to define collection of permissions for your application, sometimes you just want something simpler to control access to certain pages. The simplest way to define your own access rule without using RBAC is to make use of $matchCallback property when definining Access Control rules.

In behaviors() function in Controller or Module:

Continue reading »

Yii2: Posting Data via Link

This seems to be undocumented (or not so well documented) at the moment. Yii2 provides special supports which allows you to post data via clicking a link.

<?= Html::a('Text', 
    ['/controller/action'], [
    'data-method' => 'POST',
    'data-params' => [
        'param1' => 1,
        'param2' => 2,
    ],
]) ?>

Note:

  • Line 3: Specifying the method, eg ‘GET’, ‘POST’, ‘PUT’
  • Line 4: Data to be posted.

To get posted data in the action:

public function actionAction () {
    $param1 = Yii::$app->request->post('param1', null);
    $param2 = Yii::$app->request->post('param2', null);
}

WordPress: Extra content with Simple Fields

WordPress allows adding extra content to your post or page contents with custom fields. This is useful if the the content is simple text value. You can still use it for other types of data such as image and radio buttons. But it’d be nice if there’s a nice user interface to simplify the process, to allow different types of input and to validate the input.

Continue reading »