Have you seen our new video tutorials? Check it out!

Datetime Field Type

The datetime field type provides a date and/or time picker input.

Introduction

anomaly.field_type.datetime

The datetime field type provides a customizable date and/or time input.

Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.country",
    "config" => [
        "default_value" => null,
        "mode"          => "datetime",
        "date_format"   => "j F, Y",
        "year_range"    => "-50:+50",
        "time_format"   => "g:i A",
        "timezone"      => null,
        "step"          => 15
    ]
]
Configuration
Key Example Description

default

now

The default value.

mode

time

The input mode. Valid options are datetime, date, and time.

date_format

l jS \\of F Y

A PHP date format. Only used in datetime and date modes.

year_range

-5:+50

A range of valid years to choose from.

time_format

h:i:s A

A PHP date format. Only used in datetime and time modes.

timezone

America/Chicago

The timezone for the input and output. Defaults to system configured timezone.

step

30

The minute step detail. Only used in datetime and time modes.

Addon Configuration

The datetime configuration options are controlled by the field type's formats.php configuration file.

You can override these options by publishing the addon and modifying the resulting configuration file:

php artisan addon:publish anomaly.field_type.datetime

The field type will be published to /resources/{application}/addons/anomaly/datetime-field_type.

Contribute: If you have options to add or have found an error, submit a pull request to https://github.com/anomalylabs/datetime-field_type

Usage

This section will show you how to use the field type via API and in the view layer.

Setting Values

You can set the datetime field type value with a timestamp.

$entry->example = date("U");

You can also use any string interpretable by the strtotime method.

$entry->example = "now";

$entry->example = "+2 days";

Lastly, you can also set the value with an instance of Carbon.

$entry->example = new Carbon;

Basic Output

The datetime field type returns null or an instance of Carbon in the configured timezone.

You can learn more about Carbon's API here: http://carbon.nesbot.com/docs

$entry->example->format('l jS \\of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM;

Presenter Output

This section will show you how to use the decorated value provided by the \Anomaly\DatetimeFieldType\DatetimeFieldTypePresenter class.

DatetimeFieldTypePresenter::format()

The format method maps to the Carbon::format() method.

Returns: string
Arguments
Key Required Type Default Description

$format

false

string

The configured format.

A PHP date format.

Example
$decorated->example->format();
Twig
{{ decorated.example.format() }}

DatetimeFieldTypePresenter::date()

The date method returns the formatted date portion of the value only.

Returns: string
Arguments
Key Required Type Default Description

$format

false

string

The configured value.

A PHP date format.

Example
$decorated->example->date();
Twig
{{ decorated.example.date() }}

DatetimeFieldTypePresenter::time()

The time method returns the formatted time portion of the value only.

Returns: string
Arguments
Key Required Type Default Description

$format

false

string

The configured value.

A PHP date format.

Example
$decorated->example->time();
Twig
{{ decorated.example.time() }}

DatetimeFieldTypePresenter::local()

The local method returns the formatted value in the users active timezone.

Returns: string
Arguments
Key Required Type Default Description

$format

false

string

The configured format.

A PHP date format.

Example
$decorated->example->local();
Twig
{{ decorated.example.local() }}

DatetimeFieldTypePresenter::timeAgo()

The timeAgo ago method maps to Carbon::diffForHumans().

Returns: string
Example
$decorated->example->timeAgo();
Twig
{{ decorated.example.timeAgo() }}

DatetimeFieldTypePresenter::iso()

The iso method is a shortcut for returning the ISO 8601 formatted value.

Returns: string
Example
$decorated->example->iso();
Twig
{{ decorated.example.iso() }}

DatetimeFieldTypePresenter::rfc()

The rfc method is a shortcut for returning the RFC 2822 formatted value.

Returns: string
Example
$decorated->example->rfc();
Twig
{{ decorated.example.rfc() }}

DatetimeFieldTypePresenter::__call()

The __call magic method is mapped to Carbon.

Example
$decorated->example->toDayDateTimeString();
Twig
{{ decorated.example.toDayDateTimeString() }}