@@ -37,8 +37,9 @@ public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface
37
37
public function beforeSave ($ object )
38
38
{
39
39
$ attributeName = $ this ->getAttribute ()->getName ();
40
- $ _formated = $ object ->getData ($ attributeName . '_is_formated ' );
41
- if (!$ _formated && $ object ->hasData ($ attributeName )) {
40
+ $ pattern = '/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/ ' ;
41
+ // format only date that was not formatted yet
42
+ if ($ object ->hasData ($ attributeName ) && !preg_match ($ pattern , $ object ->getData ($ attributeName ))) {
42
43
try {
43
44
$ value = $ this ->formatDate ($ object ->getData ($ attributeName ));
44
45
} catch (\Exception $ e ) {
@@ -50,7 +51,6 @@ public function beforeSave($object)
50
51
}
51
52
52
53
$ object ->setData ($ attributeName , $ value );
53
- $ object ->setData ($ attributeName . '_is_formated ' , true );
54
54
}
55
55
56
56
return $ this ;
@@ -60,7 +60,7 @@ public function beforeSave($object)
60
60
* Prepare date for save in DB
61
61
*
62
62
* string format used from input fields (all date input fields need apply locale settings)
63
- * int value can be declared in code (this meen whot we use valid date)
63
+ * int value can be declared in code (this means that we use valid date)
64
64
*
65
65
* @param string|int|\DateTime $date
66
66
* @return string
@@ -74,9 +74,9 @@ public function formatDate($date)
74
74
if (is_scalar ($ date ) && preg_match ('/^[0-9]+$/ ' , $ date )) {
75
75
$ date = (new \DateTime ())->setTimestamp ($ date );
76
76
} elseif (!($ date instanceof \DateTime)) {
77
- // normalized format expecting Y-m-d[ H:i:s] - time is optional
78
77
$ date = $ this ->_localeDate ->date ($ date , null , false );
79
78
}
79
+ // normalized format expecting Y-m-d [H:i:s] - time is optional
80
80
return $ date ->format ('Y-m-d H:i:s ' );
81
81
}
82
82
}
0 commit comments