I am trying to convert a date from yyyy-mm-dd to mm-dd-yyyy Yii2 GridView; however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string.
[
'attribute' => 'created_at',
'format' => ['datetime', 'php:m/d/Y H:i:s']
],
Related
. Good day! Please tell me a technical question, in GridView Yii2, code:
['attribute' => 'request_date_create',
'value' => function ($model) {
return Yii::$app->formatter->asDateTime($model->request_date_create, 'php: d.m.Y H:i:s');
},
],
Does it display the date 3 hours later than the one taken from the database? And this code displays normal :
['attribute' => 'request_date_create',
'value' => function ($model) {
return Yii::$app->formatter->asDateTime(strtotime($model->request_date_create), 'php: d.m.Y H:i:s');
},
],
And this code generally displays nonsense :
[
'attribute' => 'request_date_create',
'format' => ['date', 'php: d.m.Y H:i:s']
],
Why? There is some nuance, but I cannot understand what
[
'attribute' => 'request_date_create',
'format' => ['datetime', 'php:d.m.Y H:i:s']
],
Try to use datetime format and remove whitespace after php:
Looks like PHP time zone is different. See docs https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#$defaultTimeZone-detail and example https://www.yiiframework.com/wiki/684/save-and-display-datetime-fields-in-different-formats-in-yii2#tip-3-controlling-global-formats how to set formater time zone, date and time formats
As the Laravel document, we can cast the value to DateTime.
https://laravel.com/docs/5.8/eloquent-serialization#date-serialization
protected $casts = [
'birthday' => 'date:Y-m-d',
'joined_at' => 'datetime:Y-m-d H:00',
];
There result is :
"2020-05-29 00:00:00"
but i want to result like that:
"2020-10-29T00:00"
I can use Carbon with format "Y-m-d\TH:i");
if possible to format like "2020-10-29T00:00" if I don't use the Carbon?
any ideas will be appreciated.
As the Carbon documentation (https://carbon.nesbot.com/docs/#api-formatting) states. You can use format() function.
$dt->format("Y-m-d\TH:i")
My JavaScript library datepicker returns dates in a format "March 2019."
Carbon can decode it.
$date = Carbon::createFromFormat('M Y', $request->month);
Laravel fails on date_format validation.
$request->validate([
'month' => [
'required',
'date_format:M Y',
],
]);
saying
The month does not match the format M Y.
I have tried all PHP date formats from here
: M, MM, mm, m.
$request->validate([
'month' => [
'required',
'date_format:F Y',
],
]);
You're using the wrong format. You need to use standard PHP formats, which is what Carbon and Laravel's validation uses. You can find them in the PHP Docs for date()
So change your rule to:
date_format:"F Y"
I've been stuck for days, I can't get my dates to save to my database and having issue after issue. Can someone recommend a video/article or just plain explain on how to fully deal with dates?
Migration
$table->dateTime('start_date');
$table->dateTime('end_date');
Controller
public function store(Request $request)
{
$this->validate(request(), [
'name' => 'required',
'description' => 'required',
'status' => 'required',
'startdate' => 'required',
'enddate' => 'required'
]);
Event::create(request(['name', 'description', 'status', 'start_date', 'end_date']));
return redirect('/events');
}
Model
THIS IS OBVIOUSLY THE ISSUE and I would paste things but theres multiple things I've put here and nothing works.
This is the dd of the request
+request: ParameterBag {#40 ▼
#parameters: array:6 [▼
"_token" => "yjU5A0Z5dpRES7KWK82fRMbHMRQkaooe27vYKlo7"
"name" => "My first event"
"status" => "Inquery"
"description" => "this is my desc"
"start_date" => "02/16/2017 6:36 PM"
"end_date" => "02/24/2017 6:37 PM"
]
You need to provide the dates to the database (via laravel) in a format that it understands.
You're using the d/m/Y H:i A format on the front end (see the PHP Date page for more on date formats). The database likes to receive dates in the format Y-m-d H:i:s. Even better, laravel can insert Carbon date objects into the database with no formatting on your part.
As Carbon comes with Laravel, you can use it to change your date string into a Carbon object.
$start_date = \Carbon\Carbon::createFromFormat('d/m/Y H:i A', $request->input('start_date'));
As you can see, Carbon takes the date string and as the format has been supplied, it's easy for it to create a new Carbon object. This can be directly inserted into the database.
If you really wanted, you could explicitly cast it into a string with $start_date->toDateTimeFormat();, or even $start_date->format('Y-m-d H:i:s');.
// put the first three elements in the insertData array
$insertData = request()->only(['name', 'description', 'status']);
$insertData['start_date'] = \Carbon\Carbon::createFromFormat('d/m/Y H:i A', $request->input('start_date'));
$insertData['end_date'] = \Carbon\Carbon::createFromFormat('d/m/Y H:i A', $request->input('end_date'));
// both dates are now carbon objects, not strings
Event::create($insertData);
I am using a DateControl widget by Kartik in my Yii2-powered system. The widget correctly saves the time I've selected. However, when I tried to update the data, it just always shows "12:30" as the time and not the time from the database. I am still new to Yii2 and I there's not much information on the Internet regarding this issue. Thank you for the help!
Code for my form:
<?= $form->field($model, 'class_start_time')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_TIME,
])
?>
<?= $form->field($model, 'class_end_time')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_TIME,
])
?>
Code for the config:
'displaySettings' => [
Module::FORMAT_DATE => 'dd-MM-yyyy',
Module::FORMAT_TIME => 'HH:mm a',
Module::FORMAT_DATETIME => 'dd-MM-yyyy HH:mm:ss a',
],
// format settings for saving each date attribute (PHP format example)
'saveSettings' => [
Module::FORMAT_DATE => 'php:U', // saves as unix timestamp
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
I found the solution. The displaySettings should be hh:mm a and not HH:mm a. There is a mismatch in the format which causes the display to be in error when the time is in PM (or greater than 12:00:00).