Laravel discrepancy between two date fields that should both be UTC - laravel

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

Related

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'])

Laravel 5 get current datetime in MySQL format

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'.

Laravel 4 timstamp

I have a field in mysql table with type 'TIMESTAMP' and using a calendar on my view to get date in format dd/MM/YYYY. Inserting data with following code
$billing = new Billing;
$billing->date = Input::get('date');
$billing->save();
It is filling data with 0000-00-00 00:00:00 in my field. How can i get correct value. I also tried to convert it to timestamp as followed.
$billing = new Billing;
$billing->date = date("Y-m-d H:i:s",strtotime(Input::get('date')));
$billing->save();
But it is also not working...Can any one help!!!
if I understand you correctly, mysql timestamps have to be saved as strings in YYYY-mm-dd HH:ii:ss format (ie, as returned by date('Y-m-d H:i:s')). You could convert with date('Y-m-d H:i:s'), strtotime(Input::get('date')))

date time problem with codeigniter 2

I have set mysql database field type as 'datetime'
when i send
$now = time();
echo form_hidden('a_date', unix_to_human($now));
from views all I get in database is 0000-00-00 00:00:00
please help
i usually do it like this
$now = date('Y-m-d H:i:s', strtotime('now'));
echo form_hidden('a_date', $now);
If you want to output differently, use http://www.php.net/manual/en/function.date.php
Also, if you want to record the time and not update it, I would recommend a timestamp field instead of datetime field. That way, you don't have to pass the db anything

Resources