Everyone
I am new to Laravel Lumen Framework.I am using Laravel Lumen 5.4.7 and my PHP version is 7.0.I am getting the issue while saving time zone in database using Laravel Lumen web api.I have changed my local time zone in .env file
APP_TIMEZONE=Asia/karachi
When i just echo this code
date('Y-m-d H:i:s')
or this
\Carbon\Carbon::now()
both lines of code returns me my current local time but when i save this timezone in database for "created_at" and "updated_at" fields.It saves wrong time zone.Why this is happening?I am using this line of code to save data in database
$order_data=array(
'amount'=>35,
'updated_at' =>\Carbon\Carbon::now(),
'created_at' =>\Carbon\Carbon::now()
);
DB::table('orders')->insertGetId($order_data);
I have also tried this line of code to save data
DB::table('orders')->insert($order_data);
But still got wrong time zone in database in created_at and updated_at field.Then i also updated my code to use Elequont,for this purpose i have created a model named "Order" but still got wrong time zone in database.My Elequont code is following.
$order_data=array(
'amount'=>35,
'updated_at' =>\Carbon\Carbon::now(),
'created_at' =>\Carbon\Carbon::now()
);
Order::create($order_data);
I have tried too much to remove this issue.But not get any luck yet.Can anyone help me why Lumen is not saving current time zone in database?
Try adding 'timezone' => env('APP_TIMEZONE', "UTC") to your config/app.php and running php artisan cache:clear after that.
Also try adding DB_TIMEZONE=+05:00 to your .env file (adjusted to your UTC offset of course)
Related
I'am using Carbon for manipulating dates in a laravel project.
Carbon::now('+5:30');
Above code is working fine in local environment but not in development environment.
This is what i get on dd(Carbon::now('+5:30'));
1 - IN LOCAL ENVIRONMENT php version - 5.6.3
2 - IN DEVELOPMENT ENVIRONMENT php version - 5.5.9-1ubuntu4.14
But both environment behaves same if i use timezone name instead of time-offset like,
Carbon::now('Asia/Tokyo');
Is this something about the php-version or something else?
I have an answer for you after searching for related issues.
It seems that PHP version 5.5.9 had a bug:
https://stackoverflow.com/a/14069062/5912664
So you can't use that method with Carbon, but the following should work:
Carbon::now()->addHours(5)->addMinutes(30);
You can place your servers timezone in there for added accuracy:
Carbon::now(date_default_timezone_get())->addHours(5)->addMinutes(30);
you can change in
'timezone' => 'UTC'
This time zone must match to your country zone.So replace this UTC with your current zone.
I am using rails 2.3.9, ruby 1.9.3, rubygems 1.8.24, MS SQL Server 2008 R2, and Ubuntu Server 11.04.
Here's my problem:
I am passing an xml file through curl to a web api me and my boss developed, but when the file gets processed, the time appears subtracted by eight hours. My time zone is set at GMT+8, meaning the system is auto-converting my time to UTC. How do i fix this so that my time is set to GMT+8?
Here's the XML file:
<pos-activity>
<siteId>3</siteId>
<transCode>DEP</transCode>
<supervisorCode>1235</supervisorCode>
<amount type="decimal">5000.0</amount>
<data></data>
<date type="datetime">2012-8-16 20:40:04</date>
</pos-activity>
In this example, when this file is processed the resulting time would be 12:40:04 Z
Thank you.
#sometime.in_time_zone("your time zone")
To see a list of time zones, run rake -D time
Change config.time_zone to whatever time zone you want in your environment.rb
The simple thing I want is to store a DateTime in UTC in the database and retrieve it in UTC again, but rails seems to assume I stored the DateTime in my local time zone and adds the 8 hours difference (PST). I have a DateTime field in my model. If I send a request (PUT) to update this field in a particular instance of my model, somehow timezones get mixed up for whatever reason:
Sending to rails via PUT: 2012-02-17T03:46:58Z
Returned via subsequent GET: 2012-02-17T11:46:58Z
The time difference is exactly 8 hours and could be explained by my timezone which is PST (-08:00).
I am using Datamapper 1.2 on Rails 3.1.3.
I set my application config timezone to UTC explicitly.
I also tried to use dm-zone-types. Did not change anything for me.
A git-bisect on my repo revealed, that this misbehavior was introduced as I switched the database to postgres from the original sqlite. This commit only changed the database.yml and the gemfile. Nothing else.
Any ideas?
I have found a hacky solution myself:
Set the servers timezone in the environment variable TZ to 'UTC'. In order to persist this configuration option, I decided to put the environment variable setting in config/boot.rb:
ENV['TZ'] = "UTC"
I still feel dirty and this solution still gives me the willies. So any better/cleaner solution is highly appreciated!
I'm fairly new to Ruby on Rails. I created a blog using SQLite3, because I am unable to install MySQL, and would like to display the date of creation next to the title.
The date of creation is created using a timestamp. As I am new to RoR I created the blog using generate scaffold so I'm not 100% sure how that all works.
At the moment, when I display the created_at field, I'm given an ugly format:
2011-12-05 14:11:10 UTC
Is there a way to change this so that it display DD-MM-YYYY HH:MM, or preferably to say "posted 30 days ago". I realize the latter would be a lot more tricky.
I think you're looking for strftime - ruby documentation here.
Example:
irb(main):001:0> a = Time.now
irb(main):002:0> a.strftime("%d-%m-%Y %H:%M")
=> "05-12-2011 15:08"
First of all: you will have to install something other than SQLite (I recommend PostgreSQL, not MySQL). SQLite is unsuitable for production, and you don't really want your dev and production DB servers to be different.
To your question: Ruby and Rails have lots of date formatting methods. to_s might do what you want, or there's things like ActionView::Helpers::DateHelper.time_ago_in_words.
I have some code that seems to execute improperly when it is serialized and run with delayed_job. To get the necessaries out of the way, I am running Ubuntu 11.04, Ruby 1.8.7 with Rails 3.0.4.
I have a datetime field in one of my tables that obviously stores the date and time of a particular event. In my RoR application, I use this field through the application calling strftime on it to format it in different ways. The output of this formatting is correct on the web page.
I am also using delayed_job to put this same field into an email that is sent out (triggered via some action). When the email arrives, it appears that it has somehow been formatted as GMT. For example, if the date time was supposed to be 11-12-2011 15:30:00 (3:30 PM) in the database, the email would read 11-12-2011 22:30:00 (10:30 PM).
I looked in the database and found somethings that are interesting:
The datetime in the database is 2011-11-12 22:30:00
The app, when displayed via the web, formats the data properly as 11-12-2011, 3:30 PM
The email, formats the data properly as 11-12-2011, 10:30 PM
When I run a small ruby file that simply prints the date and time
p Time.now
I get this the correct output (local, non GMT time)
Mon Aug 15 10:15:27 -0600 2011
When I look at what is in the serialized yaml for the delayed_jobs table, I can see that the date field is formatted as
2011-11-12 22:30:00 Z
In my application.rb, I have
config.time_zone = 'Mountain Time (US & Canada)'
and in my environment.rb, I have
timezone = "Mountain Time (US & Canada)"
Can any one help me figure out what is going on here? I am relatively new to RoR, so this may be a super obvious, easy question. If you need any more debug information, please let me know and I can post it up. Similarly, if my question is unclear, I can try to clar
In my case, I solved explicitly specifying the method in_time_zone in the view
example: #my_obj.created_at.in_time_zone.
I know it is not elegant, but so far I have not found better solutions.
Try changing the date object to a string before serializing.
You may want to set the timezone in the database. If it's MySQL, see: http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html