Laravel 5 get current datetime in MySQL format - laravel

In Laravel 5, how to get current DateTime in local time (without timezoning) and convert it to MySQL datetime?

use Carbon\Carbon
$mytime =Carbon::now();
echo $mytime->toDateTimeString();
see this
Carbon

For current time you in your local timezone you can use carbon in laravel
Carbon::now('Asia/Dhaka')->toDateTimeString()
and you can use it in mysql.To get timezone list go here http://php.net/manual/en/timezones.asia.php

If you are using Laravel 5.2 and want to change MySQL timezone, you'll have to edit config/database.php and add this line
'mysql' => [
...
'timezone' => '+01:00'
],
To set current DateTime in your local time for your application, go to config/app.php, find timezone end replace 'UTC' to 'Asia/Dhaka'.

Related

Laravel discrepancy between two date fields that should both be UTC

I'm on a project that uses Laravel 8.
My timezone is configured as such in my config/app.php file:
'timezone' => 'UTC',
My migration is setup like this:
$table->dateTimeTz('sent_at')->nullable();
$table->timestampsTz();
And when I create a new record for this table I do something like this:
$model = Model::create([
'sent_at' => now(),
]);
Yet when I inspect the database, or when I display the dates in Laravel Nova for example, it seems like the created_at is stored/displayed in UTC but not the sent_at. They should be identical?
I end up with:
created_at: 2022-07-02 07:29:59
sent_at: 2022-07-02 10:29:59
What am I doing wrong? I'd like both these fields to be identical.
You can get current time and date of timezone UTC using bellow lines:
$date = new DateTime("now", new DateTimeZone('UTC') );
echo $date->format('Y-m-d H:i:s');
Please check same question on stackoverflow Laravel changing timezone not reflecting the correct time

Laravel localized date formatting (pattern)

I would like to set Laravel and Carbon so that, based on the current locale selected by the user, dates will be formatted with the correct pattern. I thought it was enough to set LC_TIME on the desired locale and then use the Carbon method toDateString to get the correct format but, regardless the LC_TIME setted, it always return a date string in the format yyyy-mm-dd.
Expected results:
- If Italian selected, then mm/dd/yyyy
- If English selected, then yyyy-mm-dd
- and so on
I'm using Laravel 5.5 and Carbon 1.36.1
Carbon::now()->isoFormat('L');
Gives you the current standard digit-format date for the current locale (20/5/2020 for "it_IT", 5/20/2020 for "en_US").
And you can customize this format for a given locale in translations:
Translator::get('en')->setTranslations([
'formats' => [
'L' => 'YYYY-MM-DD',
],
]);
Recently I had the same problem with an old Laravel app and we solved it by storing the format of the localised dates in a separate language file:
resources/lang/en/dates.php
return [
'full' => 'Y-m-d'
];
resources/lang/it/dates.php
return [
'full' => 'm/d/Y'
];
When formatting a date, just use the config() helper to fetch the format provided for the language set in config/app.php, use $date->format(trans('dates.full')) and it will return the proper localised date.
If you so fancy you can use a macro (which was added in 1.26.0) too, to simplify this process:
Carbon::macro('localisedFormat', function ($key) {
return $this->format(trans("dates.{$key}"));
});
and access it via
$date->localisedFormat('full');

Laravel: Check if time + 4 hours has passed

I store in my database a date format like this:
2017-02-22 16:55:40
I added it to my database like this:
Carbon::now();
I need to check if 4 hours passed since this date.
How I can do this? I couldn't figure out how I can convert this format into Carbon or timestamp.
If you are using Laravel and the date is a Carbon instance from a Model you have access to the whole Carbon API.
You can use the Difference API of Carbon for this specific purpose.
echo $model->thedate->diffInHours($now, false);
If your model does not threat the date as a carbon instance you can cast it by adding the date to the dates array of the current model like so
protected $dates = [
'field_name',
];
Check out Date casting for more information
Update with an explicit example
$user = User::first();
// This will return the difference in hours
$user->created_at->diffInHours(Carbon\Carbon::now(), false);
You can convert it to a Carbon object with:
Carbon::parse('2017-02-22 16:55:40');

How to save date in Laravel in GMT 0?

How to save date in GTM 0 in database?
I mean this: protected $dates = ['created_at'];
I'd personally recommend leaving/setting the timezone in your app config as UTC rather than GMT:
https://www.timeanddate.com/time/gmt-utc-time.html
To make sure my custom date fields are stored correctly I use a modifier on my model class like so:
public function setStartAtAttribute($date)
{
$timezone = (Auth::check() ? Auth::user()->profile->timezone : config('pgn.phoenix.timezone'));
$tzdate = Carbon::createFromFormat('d/m/Y H:i', $date, $timezone);
$this->attributes['start_at'] = $tzdate->timezone('UTC');
}
That would need use Carbon\Carbon; adding to the top of your model too.
That config param above is set to 'Europe/London' and is the timezone that our guests are shown dates, (works with daylight savings) whilst not affecting the 'system' timezone.
You can change the timezone of the application by going to the config/app.php file and changinf the following line:
'timezone' => 'GMT'
If your dates are still saved in a different timezone, it is likely that the server datetime will need to be altered to GMT. This can be done using the following command and following the instructions:
dpkg-reconfigure tzdata

Laravel Carbon: Why is now() exactly 1 day ahead?

I am using this in a seeder:
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
And am getting the correct date/time, but it is exactly 1 day ahead.
I have tried:
Carbon::now(new DateTimeZone('America/Chicago')),
In my seeder files, and it seems to work. However, when I am inserting records from a controller, the date is incorrect.
I am using $table->timestamps(); to create the columns - is there a configuration setting that I can enter the correct time zone? Or, is there something else I am doing wrong?
By default Laravel uses 'timezone' => 'UTC', whereas your original timezone America\Chicago is 'timezone' => 'UTC-06:00'.
If you want your timezone to be fixed to America/Chicago set your timezone to
'timezone' => 'America/Chicago'
in config/app.php file.
In case if you want to set it on run time you can accomplish that by following way:
config(['app.timezone' => 'America/Chicago'])

Resources