How to set the right timezone in Laravel 4 ? - laravel-4

I went to config/local/app.php
What should I change this 'timezone' => 'UTC' to ?
If I want to set it for Eastern Time US.

EST is UTC-5. Intuitively you'd think you want to change it to UTC-5. So:
'timezone' => 'UTC-5'
However this might give you an error. If UTC-5 does not work, try:
'timezone' => 'America/New_York'
(or if you like things further north, 'America/Toronto')
See: http://www.timeanddate.com/time/zones/est

Related

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

Converting javascript time with php Carbon returns strange time?

I'm setting my time using javascript when creating/updating my cookie.
// get current time
let d = new Date();
let time = d.getTime();
Which variable time becomes this string...
1586186947954
The number above should result 16:29 GMT 6 April 2020
But when I run this number with carbon...
use Carbon\Carbon;
$updated = Carbon::parse($cart['updated']['time']);
This is returned...
Carbon\Carbon Object
(
[date] => 52234-04-07 05:32:34.000000
[timezone_type] => 1
[timezone] => +00:00
)
Which is way off for reason. Minutes and the date are no where near.
I'm trying to output this timestamp in the timezone Asia/Dubai too if I can get Carbon to return the right time anyway.
Any ideas would be great thanks.
The parse method is for parsing more complex strings, so it is misinterpreting the value you're passing.
You should instead construct the object like this:
Carbon::createFromTimestampMs(1586186947954, 'Asia/Dubai')

set a due date dynamic

I would like to set a due date (dynamic), the person can register if it is 17to 60 years old.
I have tried this:
'date_naissance' => 'required|date|after:1960|before:2003-01-01'
But, I have to change the values each years.It is possible to improve this?
Thank you
You can try something like this:
$year = (int)date("Y");
'date_naissance' =>
'required|date|after:'.($year - 59).'|before:'.($year - 16).'-01-01'
Or a bit more precise:
'date_naissance' =>
'required|date|after:'.date('Y-m-d', strtotime('-59 year')).'|before:'.date('Y-m-d', strtotime('-16 year'))
It should work also with this:
'date_naissance' =>
'required|date|after:-59 year|before:-16 year'))
You would need to make custom validation rule. Here is example how to do it:
LINK
You will need to make method that takes person birth date and calculate his age. If age is greater then 17 it should return true.

How do you set a default format for dates in a hash? [duplicate]

I am trying to return a date with this format
2015-10-07T00:32:50.877+0000
I have tested that
Time.now.iso8601
=> "2015-10-21T09:47:50-04:00"
but i didn't have same format
tks
You can use strftime yourself and create the format you want as described here
The format you specified should be %Y-%m-%dT%H:%M:%S.%L%z
And so the complete Ruby statement would be Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
Various ISO 8601 formats:
%Y%m%d => 20071119 Calendar date (basic)
%F => 2007-11-19 Calendar date (extended)
%Y-%m => 2007-11 Calendar date, reduced accuracy, specific month
%Y => 2007 Calendar date, reduced accuracy, specific year
%C => 20 Calendar date, reduced accuracy, specific century
%Y%j => 2007323 Ordinal date (basic)
%Y-%j => 2007-323 Ordinal date (extended)
%GW%V%u => 2007W471 Week date (basic)
%G-W%V-%u => 2007-W47-1 Week date (extended)
%GW%V => 2007W47 Week date, reduced accuracy, specific week (basic)
%G-W%V => 2007-W47 Week date, reduced accuracy, specific week (extended)
%H%M%S => 083748 Local time (basic)
%T => 08:37:48 Local time (extended)
%H%M => 0837 Local time, reduced accuracy, specific minute (basic)
%H:%M => 08:37 Local time, reduced accuracy, specific minute (extended)
%H => 08 Local time, reduced accuracy, specific hour
%H%M%S,%L => 083748,000 Local time with decimal fraction, comma as decimal sign (basic)
%T,%L => 08:37:48,000 Local time with decimal fraction, comma as decimal sign (extended)
%H%M%S.%L => 083748.000 Local time with decimal fraction, full stop as decimal sign (basic)
%T.%L => 08:37:48.000 Local time with decimal fraction, full stop as decimal sign (extended)
%H%M%S%z => 083748-0600 Local time and the difference from UTC (basic)
%T%:z => 08:37:48-06:00 Local time and the difference from UTC (extended)
%Y%m%dT%H%M%S%z => 20071119T083748-0600 Date and time of day for calendar date (basic)
%FT%T%:z => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended)
%Y%jT%H%M%S%z => 2007323T083748-0600 Date and time of day for ordinal date (basic)
%Y-%jT%T%:z => 2007-323T08:37:48-06:00 Date and time of day for ordinal date (extended)
%GW%V%uT%H%M%S%z => 2007W471T083748-0600 Date and time of day for week date (basic)
%G-W%V-%uT%T%:z => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended)
%Y%m%dT%H%M => 20071119T0837 Calendar date and local time (basic)
%FT%R => 2007-11-19T08:37 Calendar date and local time (extended)
%Y%jT%H%MZ => 2007323T0837Z Ordinal date and UTC of day (basic)
%Y-%jT%RZ => 2007-323T08:37Z Ordinal date and UTC of day (extended)
%GW%V%uT%H%M%z => 2007W471T0837-0600 Week date and local time and difference from UTC (basic)
%G-W%V-%uT%R%:z => 2007-W47-1T08:37-06:00 Week date and local time and difference from UTC (extended)
If you just want to include the fractional number of seconds, you can use DateTime#iso8601([n=0]) → string:
pry(main)> require 'date'
pry(main)> DateTime.now.iso8601(3)
=> "2017-01-17T12:31:26.695+11:00"
Or in Rails:
pry(main)> Time.now.iso8601(3)
=> "2017-01-17T12:31:26.695+11:00"
It seems like the main issue is that you want the output to be UTC encoded ISO 8601 timestamp, while Ruby by default uses the timezone of the Time value - which is most likely, especially if you create the value using Time.now, your local time zone.
The solution would be to convert the Time value to a UTC timezone using #utc method:
now = Time.now
# convert to UTC and format
puts now.utc.iso8601
This would output 2020-04-20T20:46:31Z - the Z there is equivalent to +00:00 and means UTC (or "Zulu time" in US military jargon). Any ISO-8601 compliant implementation should accept Z being identical to +00:00.
Note:
The iso8601 method on the Time instance is an extension. It is part of the standard library, but you have to load it explicitly using require 'time'. This also loads other useful extensions for the Time class.
Did you try with Date?
Date.new(2018,5,25).iso8601 # returns "2018-05-25"
For today, it would be:
Date.today.iso8601
For a UTC time in ISO 8601:
require 'date'
DateTime.now.new_offset(0).iso8601
# => "2022-04-15T16:33:17+00:00"
(This works even outside of Rails.)

How can I group on the created_at field and return only the Date and hour

I run the following in Rails:
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'DATE(created_at)')
which returns a hash with the items nicely grouped.
However I want to group not by date only by date but by the hour, but not the minutes or seconds.
How can I run this query so all the heavy lifting is done by Postgresql and not convert after I get the data set back?
My mistake I want to group by Date and Hour. Sorry for the confusion.
It sounds like you want the date_trunc() function:
http://www.postgresql.org/docs/current/interactive/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
I want to group not by date but by the hour, but not the minutes or
seconds.
Bold emphasis mine.
So date_trunc()is probably not what you want, but EXTRACT or date_part():
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'EXTRACT (hour FROM created_at)')

Resources