country time in laravel system - laravel

I work on a course system that has course times I want when the site is opened in a country, the time of the course appears for the user in the real time of the country in which the user is located

To convert user time to UTC use following laravel code in your controller method
# convert user date provided date string to user date
$user_date = date('Y-m-d H:i:s', strtotime($date_string));
# convert user date to utc date
$utc_date = Carbon::createFromFormat('Y-m-d H:i', $user_date, $user->timezone);
$utc_date->setTimezone('UTC');
# check the utc date
dd($utc_date);
Convert UTC time to User time
# using utc date convert date to user date
$user_date = Carbon::createFromFormat('Y-m-d H:i', $utc_date, 'UTC');
$user_date->setTimezone($user->timezone);
# check the user date
dd($user_date);

Related

Adding custom time to date

the timestamp is in a format that I am not able to add to the ruby date. How can I do this?
sd=Date.parse('2016-01-01')
z="4:00am"
How do I add z into sd?
You can't add time to date but you can parse time like this (just concatenate date and time)
date = '2016-01-01'
time = '4:00am'
require 'time'
Time.parse("#{date} #{time}")
# => 2016-01-01 04:00:00 +0300
To avoid some parsing misinterpretation you can explicitly point directives
DateTime.strptime("#{date} #{time}", '%Y-%m-%d %H:%M%p')

Change timezone with Carbon

I have a date that is coming from database (type date):
2018-08-25
This date is in french timezone.
When I do:
$from = Carbon::parse("2018-08-25", 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-24 22:00:00.0 UTC (+00:00)
Which is what I want
But when I use field from DB:
$operation = Operation::findOrFail($request->operation);
$from = Carbon::parse($operation->date_ini, 'Europe/Paris');
$from->timezone('UTC');
dd($from);
I get:
date: 2018-08-25 00:00:00.0 UTC (+00:00)
In my DB, field is saved as : 2018-08-25, so literraly, it means 2018-08-25 UTC. So result is coherent. But I'm not sure how to deal with it to get what I want. The implication would be that I have to store my date like a datetime in DB so that I can store it in UTC with 1 or 2 hours less. Is there anyway to avoid this and keep it simple ?
Any idea ?
I solved it using:
$from = Carbon::parse($operation->date_ini)->shiftTimezone('Europe/Paris');;
shiftTimezone with change timezone without changing the date. So, it do the trick for me !
If you call setTimezone() on an existing instance of Carbon, it will change the date/time to the new timezone, for example
$changeTimeZone = \Carbon\Carbon::parse($operation->date_ini)->setTimezone('Asia/Dhaka')->format('H:i');

Double time specification in file in laravel api

I am making api's in laravel and getting 2021-01-30T10:30:17.704 05:30 from $request->followup and i have column in database named followup having datetime datatype. But it's giving me following error.
Carbon\Exceptions\InvalidFormatException: Could not parse '2021-01-30T10:30:17.704 05:30': DateTime::__construct(): Failed to parse time string (2021-01-30T10:30:17.704 05:30) at position 24 (0): Double time specification in file D:\xampp2\htdocs\synocrm-baid\rest-apis\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php on line 188
I tried to change format like
$followupDate = date('Y-m-d h:i:s A',strtotime($request->followup));
My modal having
#property Carbon|null $followup
$request->followup is appending datetime with datetime
Original Value: 2021-01-30T10:30:17.704 05:30
Datetime: 2021-01-30T10:30:17.704
Time: 05:30
As we can see, there is two Time (05:30 and 10:30:17), because of which the strtotime() cannot convert the value.
In order to fix the problem,
Send the value as time time only (2021-01-30T10:30:17.704) instead of two times Time attribute
If you have no control over request value, you can retrieve only date value like:
$dateValue = explode(' ', $request->followup)[0];
$followupDate = date('Y-m-d h:i:s A',strtotime($dateValue));
Or Using Carbon
$followupDate = Carbon::parse($dateValue);
I would not recommend option 2 because it creates problems and confusion for other devs.
2021-01-30T10:30:17.704 05:30
It does not seem to valid timestamp. For parsing, it should be the valid timestamp or date.

Date/Time error when entering into azure incorrectly

I'm trying to add 2 DateTime fields which are stored as strings in Azure. The first datetime is supposed to get the current date and time now and this stores the date time as 12/02/2019 09:01 correctly, but currently the second field is supposed to get the current date and time and add 30 minutes but it is storing is like only the date eg 12/02/2019. This is my current code:
symptomFeedback.DateTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
symptomFeedback.Datetimelimit = DateTime.Now.AddMinutes(30).ToString("dd/MM/yyyy HH:mm");

Ruby on Rails 3 convert time to DateTime in specific timezone

I have column which stores only time. I want to convert to it into datetime and then to a specific timezone. When I do it on console using only the attribute, it shows correct conversion value. But when I use it in query the query shows the current time in UTC. Suppose I have time "05:15" stored in Central time. Now when I want to fetch records for a interval of 30 minutes plus and minus of this time, I do following,
day = Time.now
# departure_time_from _db below is the name of column in table which store time
departure = departure_time_from _db.change(:day => date.day, :month => date.month, :year => date.year)
departure = departure.in_time_zone("Central Time (US & Canada)")
When I execute above code on console and see the value, it shows correct converted value. But when I use this variable in below query, current time gets used instead of the departure time.
Model.where("column_name > ? and "column_name < ?, departure - 30.minutes, departure + 30.minutes)
The time used in above query should be the time I want i.e Central time zone, but in query output, it shows UTC being used.
Please let me know what i am missing.
It shouldn't matter what time zone the query uses in the where clause as long as it's the same time representation. The time you're seeing in your query output from Rails is most likely the UTC equivalent of the ruby DateTime object being passed to the where statement. So if departure is 12:00 noon (UTC -6) in your Ruby code then the time shown in the SQL query will be 18:00, which corresponds to noon CST.

Resources