メインコンテンツまでスキップ
Version: Fegg 5.x(開発版)

Introduction

Providing some functions for calculating dates

How to use​

Import Fegg\Tool\Date and create it with new.

Controller
<?php
namespace Site\Controller;

defined('FEGG_CORE_DIR') || exit;

use Fegg\Tool\Date;

class Event extends Controller
{
public function index()
{
$date = new Date();

$this->page['deadline'] = $date->addDay(date('Y-m-d H:i:s'), 7, 'Y/m/d');
$this->page['last_day'] = $date->getTheLastday(date('Y-m-d'));

$this->displayPage('event');
}
}

Date formats​

The $dateTime arguments accept YYYY-MM-DD HH:MM:SS and its shorter forms, with or without the separators (2024, 2024/04, 2024-04-01, 20240401, 2024/4/1 9:30), see Date::makeupDateFormat. Other formats are parsed with strtotime(). Date::addDay, Date::addMinute, Date::addMonth, Date::getTheLastday and Date::getDiff (in days) throw InvalidArgumentException when the value cannot be parsed.

Time zone​

The time zone of the application is set once at startup, from $settings['timezone'] in site/config/settings.php (default FEGG_DEFAULT_TIMEZONE, Asia/Tokyo):

htdocs/_fegg/site/config/settings.php
$settings['timezone'] = 'Europe/Paris';

date(), Fegg\Tool\Date and the template modifier feggDate all use it. No Fegg method changes it afterwards:

  • Date::getSocialTime uses its $timezone argument for that call only.
  • The controller methods $this->getDatetime($format = 'Y-m-d H:i:s', $timeZone = '') and $this->convertDatetime($date, $fromTimezone, $toTimezone) work on their own DateTime objects. Fegg 4 changed the process-wide time zone in them.
  • convertDatetime() returns '' for a date it cannot read (Fegg 4 returned 1970-01-01 ...).
Controller
echo $this->getDatetime('Y-m-d H:i', 'UTC'); // now, in UTC
echo $this->convertDatetime('2026-04-01 09:00:00', 'Asia/Tokyo', 'Europe/London'); // 2026-04-01 01:00:00
echo $this->convertDatetime('not a date', 'Asia/Tokyo', 'UTC'); // ''

Methods​