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'])
Related
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
I have a one to many relationship which is using a pivot table to attach related models.
The standard timestamps are working correctly and the created_at / updated_at are getting populated with the dates when the relationshi gets created or modified.
However, I have a case where I would like to override the timestamps values.
I would like to do something like this :
$user->tickets()->attach($ticket->id)
->withTimestamps(['created_at' => '2021-01-01 00:00:01','updated_at' => '2021-01-01 00:00:01']);
Can anyone please help ?
As confirmed by #N69S, the correct syntax to include or override column values during attach is to pass an associative array of nested columns:
$user->tickets()->attach([
$ticket->id => [
'created_at' => '2021-01-01 00:00:01',
'updated_at' => '2021-01-01 00:00:01'
]
]);
This should add a record to your pivot table with the correct User ID, Ticket ID and overridden timestamps.
is there anyway to validate incoming unixtime's parameter to server and compare that second one is greather that first one by laravel validation?
my code only validate date format
$this->validate($request, [
'start_date' => 'required|date',
'end_date' => 'required|date|after_or_equal:start_date',
]
A timestamp is a number and can be anything between 0 and 2147483647. That's all you can validate. (And of course that one is bigger than the other.)
So, you could do the following:
$this->validate($request, [
'start_date' => 'required|numeric',
'end_date' => 'required|numeric|gte:start_date',
]);
numeric will make sure the input is a number
gte checks if the value is greater than or equal to another number.
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'.
I do:
$this->post('/v1/customer', ['area' => 'kil'])
->seeJson(['created' => true]);
But instead of created => true, I would like to do "NOT STATEMENTS".
Ex: parent!=null or created_at > '0000-00-00'
How can this be achieved?
Laravel does have a dontSeeJson function which would solve both of the examples you've listed (though possibly not a more general case) --
$this->dontSeeJson(['parent' => null]);
$this->dontSeeJson(['created_at' => '0000-00-00']);
If you need something more specific, I agree with #gontrollez - decode the json (json_decode($this->response->getContent(), true)) and test that.