laravel how to convert persian date to datetime format - laravel

I have a persian datepicker in my view
<div class="input-group-addon" data-mddatetimepicker="true" data-targetselector="#exampleInput1" data-trigger="click" data-enabletimepicker="true">
<span class="glyphicon glyphicon-calendar"></span>
</div>
<input type="text" class="form-control" id="exampleInput1" name="start_at"/>
and i have a column in my table that in datetime format
when I send this to controller and dd($request['start_at']); it returns "۱۳۹۷/۰۷/۲۳ ۰۰:۰۰:۰۰"
How to convert this to datetime format to save in database?

Intro
Everything that is related to date or time in Laravel should be done with Carbon.
Carbon is a PHP Library/Extension to handle the formatting of Date and Time. You can find there docs over here. Carbonis already included in Laravel, so check the API out over here.
Getting things done
What you need to do is, pass in your timezone here:
$date = Carbon::now($start_at)->locale('UTC+6');
Assuming that the persia timezone is UTC+6the above snippet converts your time to a carbon time.
After this you can access your timestamp with
$date->timestamp;
Or your DateTimeString with
$date->toDateTimeString();
Please note: I am not absolutely sure about the time settings in persia. From a quick research I would assume that is UTC+6. You can also put in a string in the locale() function like:
$date = Carbon::now()->locale('fr_FR');
if this is easier to you!
Quickshot URLs:
Laravel Carbon API
Carbon API

use this package of laravel to convert date Persian to DateTime
laravel persian datetime package

Related

How to select format date using carbon?

How to select format date using carbon ?
I'm used format ->select(DB::raw("(DATE_FORMAT(score_dt, '%M'))")) before.
I want to change the format by using carbon, how do I use it in a select query? to display month name
Have a read of the Carbon docs. Laravel uses Carbon 2 now.
You should be able to get the month name with the monthName property: $yourDateObject->monthName.
Carbon, however, is a PHP library and is not part of the database you're using. This means you can't use it in your select query, to use Carbon, you'll need to format your data in your model/controller/frontend. This is a fairly standard way of turning ISO8601 dates into human readable form.

How to change timezone using date() and strtotime() in Laravel

I'm still new in Laravel. I have a problem in understanding the concept of Carbon and the format for date and time. Everytime I create new post, the created_at column does not show the exact time as in my location. For example, currently in Kuala Lumpur the time is 2:36 PM but in my phpMyAdmin, it shows 6:36 AM. How can I change the timezone? And I'm using this code <span> {{date('d/m/Y g:i A', strtotime($comment->created_at))}} </span>
Here is what I already tried.
in my view blade.php
<span> {{ date('d/m/Y g:i A', strtotime($comment->created_at))->setTimezone('Asia/Kuala_Lumpur')}} </span>
but it gives an error Call to a member function setTimezone() on string
comments table
created_at - datetime
Hope anyone can teach and show me the correct one. Thank you.
You can set your app time zone by configuring app.php file in config folder .
To change time zone , modify the value of timezone in app.php file.
This is written in this section
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
For me i am using Asia/Kuala_Lumpur as my application time zone.
Here is the appropriate syntax :
'timezone' => 'Asia/Kuala_Lumpur'
list of timezones for PHP
NOTE : run php artisan config:clear to effect this changes

Vue.js - Laravel Populating datetime-local input field

I'm trying to populate my Vue.js Template to update a model
<input type="datetime-local" name="start_time" v-model="start_time">
Having issues pre filling the date-time field
start_time: "2020-02-12 22:00:00"
this is the data that i receive from api and i'm directly assigning it to my start_time variable
Just adding to the answer
use format
'YYYY-MM-DDTHH:mm'
otherwise there will be an issue with AM and PM
According to the MDN web docs for datetime-local the format should be yyyy-mm-ddThh:mm. Therefore you'll need to update the value of your data property to be:
2020-02-12T22:00
Since you've using moment you can format the initial date with:
moment(dateString).format('YYYY-MM-DDThh:mm')
Replace dateString with the actual date string or property that contains the date string.

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

Resources