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"
Related
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']
],
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 code:
$data['from'] = '2020-03-20 20:30:00';
$data['to'] = '2020-03-21 00:45:00';
$validity = \Validator::make($data, [
'from' => ['date_format:Y-m-d H:i:s'],
'to' => ['date_format:Y-m-d H:i:s']
]);
// This if gets true, and the error message is:
The to does not match the format Y-m-d H:i:s
if($validity->fails()) {
dd($validity->errors());
}
and amazingly when I change 00:45:00 to 01:45:00 it doesn't get inside that if. How can I fix it?
Since your locale is set up to be Asia/Tehran the time 2020-03-21 00:45:00 is actually not valid. According to https://www.timeanddate.com/time/change/iran/tehran:
Saturday, 21 March 2020, 00:00:00 clocks are turned forward 1 hour to
Saturday, 21 March 2020, 01:00:00 local daylight time instead.
This means that times between 00:00:00 and 01:00:00 never occur. Since the Laravel date validator internally uses PHP's date parsing then that parsing fails and the error is raised. If you want to check only if a date is a given format ignoring any daylight savings quirks then you can use date_default_timezone_set to temporarily set the locale to one that does not observe daylight savings.
Try
$validity = \Validator::make($data, [
'from' => 'date_format:Y-m-d H:i:s',
'to' => 'date_format:Y-m-d H:i:s'
]);
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).