How to properly store a DateTime into a database with Timezone in Laravel - laravel

My vue Js component is outputting a date in the format below. The component is actually making use of FlatPickr
2017-03-04T01:23:43.000Z
I have done quite abit of research and can see that the ISO time is in the format
2021-05-16T08:09:42+0000
I.e without the trailing 'Z' and also the seperator is a Z '.' and not a '+'
Will I need something like moment to convert the date into a suitable format to store in Laravel?
My column on my database is a DateTime column.

why dont you use carbon to input time directly from the database as you save the rest of the data.
'created_at' => \Carbon\Carbon::now(),
'updated_at' => \Carbon\Carbon::now(),
``

Related

Is it possible to prevent the time field to expect seconds in Laravel Backpack?

I'm working with Laravel Backpack and for a specific model I want the user to be able to add a "start time" using the time field type. I want the user to enter this format: hh:mm.
While creating the entity it works fine. But in the edit mode the value of the input field has this format: hh:mm:ss
The field is declared like this:
[
'name' => 'start_time',
'label' => "Beginn",
'type' => 'time'
],
The corresponding field in the DB looks like this:
$table->time('start_time')->nullable();
When the model is created this works as expected what means, that I can enter the time in hh:mm format. For example I enter "14:00".
After saving the model, in the DB I have the value stored as 14:00:00.
Now, when I go to the edit view the value in the corresponding time input field is also 14:00:00.
I assume this is because in the DB the value is stored including the seconds.
Is it possible to store a time in the DB without the seconds? So just in the format hh:mm?
For more details I have some screenshots that show what I meant:
On creation
enter image description here
enter image description here
The value in DB after saving
enter image description here
The input after loading the edit view
enter image description here
I think you can make it work by defining the step attribute in the field https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time#using_the_step_attribute
Cheers

how to add two 'time' datatypes in laravel?

I have a 'services' table having a column called 'duration'. Its datatype is time and data stored in this column like '00:30:00'
I also have another table for bookings having two columns 'start_time' and 'end_time'. Both columns datatype are also 'time'. I want to get summation of service->duration + booking->start_time and store it in the 'end_time' column. How can I add these two time data?
I tried something like this:
date('H:i:s', strtotime($booking_service->start_time) + strtotime($service->duration))
but it considers timezone offset and returns wrong result. How should I remove timezone offset? is there another better way such as using Laravel Carbon to sum these two times?

Laravel 5.8 - Validate future date

How do I validate that a given date is in the future and not a past date? Alternatively, does Html 5 allows one to disable past date from a date field?
I have a form in my app that allows user to select start date for an event from an HTML date input field. I want users to only choose a date in the future and not a past date.
I think you can use it like this:
$rules = [
'start_date' => 'date_format:d/m/Y|after:3/13/2019',
];
See also the laravel documentation:
https://laravel.com/docs/5.8/validation#rule-after
or
https://laravel.com/docs/5.8/validation#rule-after-or-equal
after:today works as #Tim Lewis commented above.
As for HTML validation, date fields still aren't fully supported. Safari being the main culprit.
You can use min="2020-01-01" format against the input, but you'll need to either add that date server-side or using JavaScript so it's tomorrow's date.
I have tried code and found that if you want to validate only year than you can write something like
$rules = ['dob'=>'required|date_format:Y|before:today']
where,
date_format
is fixed keyword and with help of 'Y' you can validate only year
and before:today validate if year is older than today or not.
you can use d-m-Y format instead only Y.
happy coding...

date_format not validating in Laravel

I am using this validation for dates:
'fiscal_end' => 'required|date_format:dd/mm/yyyy'
Doing:
print_r(Input::all());
die();
I get:
[fiscal_end] => 25/01/2017
I don't understand what's wrong here that generates the error: Date needs to be in proper format dd/mm/yyyy.
I am using Laravel 5.3 and datefield on form is actually Bootstrap Datepicker field.
You are doing with wrong format dd/mm/yyyy
Change your format as d/m/Y

How can I change the timezone of the outputted date in Laravel 4?

I have a database with an entity table and a users table. The entity table has fields for created_at and updated_at. The users table has a timezone field with a default value of America/Toronto.
In my view, I can output the date in the format I want with the following code:
$entity->created_at->format('M j, Y \\a\\t g:i A')
Now, how can I output the date using the timezone field in the users table?
Something like ...
$entity->created_at->format('M j, Y \\a\\t g:i A', Auth::user()->timezone);
I am looking for a simple way to do this with Carbon (since I am using Eloquent in Laravel, which will convert the above date columns to instances of Carbon).
Note: I only want the timezone to be changed when presenting the date - I still want all dates to be stored in the default UTC timezone in the database.
Also, where can I find a valid list of time zones that can be used with Carbon? I would like to store these in my database so users can change their default time zone.
You need to use the Carbon function copy() to keep the timezone unchanged on your object. Then you can just set the timezone to whatever you want and 'output' the result.
$entity->created_at->copy()->tz(Auth::user()->timezone)->format('M j, Y \\a\\t g:i A');
As for timezones - it is just PHP timezones.
$tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

Resources