Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Datetime Class Reference
Inheritance diagram for Datetime:
AbstractBackend BackendInterface Startdate

Public Member Functions

 __construct (\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
 
 beforeSave ($object)
 
 formatDate ($date)
 
- Public Member Functions inherited from AbstractBackend
 setAttribute ($attribute)
 
 getAttribute ()
 
 getType ()
 
 isStatic ()
 
 getTable ()
 
 getEntityIdField ()
 
 setValueId ($valueId)
 
 setEntityValueId ($entity, $valueId)
 
 getValueId ()
 
 getEntityValueId ($entity)
 
 getDefaultValue ()
 
 validate ($object)
 
 afterLoad ($object)
 
 beforeSave ($object)
 
 afterSave ($object)
 
 beforeDelete ($object)
 
 afterDelete ($object)
 
 getAffectedFields ($object)
 
 isScalar ()
 

Protected Attributes

 $_localeDate
 
- Protected Attributes inherited from AbstractBackend
 $_attribute
 
 $_valueId
 
 $_valueIds = []
 
 $_table
 
 $_entityIdField
 
 $_defaultValue = null
 

Detailed Description

@api

Since
100.0.2

Definition at line 13 of file Datetime.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Framework\Stdlib\DateTime\TimezoneInterface$localeDate@codeCoverageIgnore

Definition at line 24 of file Datetime.php.

25  {
26  $this->_localeDate = $localeDate;
27  }

Member Function Documentation

◆ beforeSave()

beforeSave (   $object)

Formatting date value before save

Should set (bool, string) correct type for empty value from html form, necessary for further process, else date string

Parameters
\Magento\Framework\DataObject$object
Exceptions

Implements BackendInterface.

Definition at line 39 of file Datetime.php.

40  {
41  $attributeName = $this->getAttribute()->getName();
42  $_formated = $object->getData($attributeName . '_is_formated');
43  if (!$_formated && $object->hasData($attributeName)) {
44  try {
45  $value = $this->formatDate($object->getData($attributeName));
46  } catch (\Exception $e) {
47  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid date'));
48  }
49 
50  if ($value === null) {
51  $value = $object->getData($attributeName);
52  }
53 
54  $object->setData($attributeName, $value);
55  $object->setData($attributeName . '_is_formated', true);
56  }
57 
58  return $this;
59  }
__()
Definition: __.php:13
$value
Definition: gender.phtml:16

◆ formatDate()

formatDate (   $date)

Prepare date for save in DB

string format used from input fields (all date input fields need apply locale settings) int value can be declared in code (this meen whot we use valid date)

Parameters
string | int | \DateTimeInterface$date
Returns
string

Definition at line 70 of file Datetime.php.

71  {
72  if (empty($date)) {
73  return null;
74  }
75  // unix timestamp given - simply instantiate date object
76  if (is_scalar($date) && preg_match('/^[0-9]+$/', $date)) {
77  $date = (new \DateTime())->setTimestamp($date);
78  } elseif (!($date instanceof \DateTimeInterface)) {
79  // normalized format expecting Y-m-d[ H:i:s] - time is optional
80  $date = new \DateTime($date);
81  }
82  return $date->format('Y-m-d H:i:s');
83  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

Field Documentation

◆ $_localeDate

$_localeDate
protected

Definition at line 18 of file Datetime.php.


The documentation for this class was generated from the following file: