How to convert date/time in multilingual Cakephp 3 website - php

I have a multilingual website in both English and Danish.
Everything works fine, but when I am on the Danish version the date/time is incorrect.
Can you please help me how can I get correct date/time in Danish the translation.
[birth_time] => Cake\I18n\FrozenTime Object
(
[time] => 2017-06-27T11:20:00+00:00
[timezone] => UTC
[fixedNowTime] =>
)

Danish? Well I think that is Denmark, in bootstrap in config, maybe you should config:
date_default_timezone_set('Europe/Copenhagen');
or time zone that you using should be:
[birth_time] => Cake\I18n\FrozenTime Object
(
[time] => 2017-06-27T11:20:00+00:00
[timezone] => UTC+01:00
[fixedNowTime] =>
)
now for set or get should create instances:
use Cake\I18n\Time;
// Create from a string datetime.
$time = Time::createFromFormat(
'Y-m-d H:i:s',
$datetime,
'Europe/Copenhagen'
)

Related

PHP Timezone bug: adding extra hour

I have problem...
I'm currently working with "America/Santiago" timezone, and whenever I create a datetime object with a hour < 01:00AM, it adds 1 hour.
Example:
date_default_timezone_set('America/Santiago');
$dateTest = date_create_from_format('d/m/Y H:i:s', '04/09/2022 00:32:27');
print_r($dateTest);
This prints:
DateTime Object
(
[date] => 2022-09-04 01:32:27.000000
[timezone_type] => 3
[timezone] => America/Santiago
)
But if I create the following object:
date_default_timezone_set('America/Santiago');
$dateTest = date_create_from_format('d/m/Y H:i:s', '04/09/2022 01:22:11');
print_r($dateTest);
prints:
DateTime Object
(
[date] => 2022-09-04 01:22:11.000000
[timezone_type] => 3
[timezone] => America/Santiago
)
I'm really lost here, can someone guide ?
Thanks

CalendarView get only today's events

I am working with the graph in PHP, and building a calendar widget for a site that should pull only today's events, including all day events.
Everything works, however it also pulls all day events for yesterday and I cannot figure out what I am doing wrong.
$today = new DateTime( 'now', new DateTimeZone( 'America/New_York' ) );
$tomorrow = new DateTime( 'tomorrow', new DateTimeZone( 'America/New_York' ) );
$start_date = $today->format('Y-m-d');
$end_date = $tomorrow->format('Y-m-d');
$events = ms_get_data_as_json( 'calendarview?startdatetime=' . $start_date . '&enddatetime=' . $end_date . '&$orderby=start/DateTime' );
the formatted request string outputs as calendarview?startdatetime=2018-05-18&enddatetime=2018-05-19&$orderby=start/DateTime which would appear to work correctly, however sending this request, the first object returned is an all day event for the previous day. See sample output:
[0] => stdClass Object
(
[createdDateTime] => 2018-05-18T13:30:37.8672462Z
[lastModifiedDateTime] => 2018-05-18T13:34:25.2248155Z
[categories] => Array
(
)
[originalStartTimeZone] => UTC
[originalEndTimeZone] => UTC
[reminderMinutesBeforeStart] => 1080
[isReminderOn] =>
[hasAttachments] =>
[subject] => Test yesterday
[bodyPreview] =>
[importance] => normal
[sensitivity] => normal
[isAllDay] => 1
[isCancelled] =>
[isOrganizer] => 1
[responseRequested] => 1
[seriesMasterId] =>
[showAs] => free
[type] => singleInstance
[onlineMeetingUrl] =>
[recurrence] =>
[responseStatus] => stdClass Object
(
[response] => organizer
[time] => 0001-01-01T00:00:00Z
)
[body] => stdClass Object
(
[contentType] => html
[content] =>
)
[start] => stdClass Object
(
[dateTime] => 2018-05-17T00:00:00.0000000
[timeZone] => UTC
)
[end] => stdClass Object
(
[dateTime] => 2018-05-18T00:00:00.0000000
[timeZone] => UTC
)
)
The start property is clearly for the prior date.
I've tried to change my query to include timestamps startdatetime=2018-05-18T00:00:01Z&enddatetime=2018-05-18T23:59:59Z yet I still get the same results. I've tried to change my $today and $tomorrow variables to use UTC, as the returned result says OriginalStartTimeZone is UTC, but I still get the same results.
Any help is appreciated.
I think there's a bit of an inequality problem in the way the apply the start/end dates filters, and it ends up including events that end on the start time (or start on the end time).
At the moment, it seems the solution is to add 1 second to the filter's start time, and subtract 1 second from the filter's end time. You'll still get all-day events for the requested date, but not all-day events for the previous or next day.
Let us know if you find situations which don't work with this method!

Cakephp 3 giving date and time fields in frozentime object

I am using cakephp 3.2 and when i am retrieving data by find query it is giving date fields in this format
Array
(
[0] => Cake\I18n\FrozenDate Object
(
[date] => 2016-08-01 00:00:00
[timezone_type] => 3
[timezone] => UTC
)
)
and time fields in frozentime
Cake\I18n\FrozenTime Object
(
[date] => 2016-10-11 10:00:00
[timezone_type] => 3
[timezone] => UTC
)
I need a common setting or global solution for complete site. So when i fetch the data by find query from database it should give me date time in simple format without any frozendate object.
like this
Array(
[0] => 2016-08-01
)
Simply call ->format('Y-m-d') on your Cake\I18n\FrozenDate object.
There's no need for Cake\I18n\FrozenDate::setToStringFormat() or $this->Time->format()
You can also use TimeHelper for formating datetime in View
Example
echo $this->Time->format(
$YourDateTimeVariable, #Your datetime variable
'Y-MM-d' #Your custom datetime format
);
CakePHP TimeHelper function details is Here
In boostrap.php add
Cake\I18n\FrozenDate::setToStringFormat('yyyy-MM-dd');
still it comes with forzenDate object with same params But when you will print in view then it will print the proper format
echo $var->created; // print: 2016-08-01
Reference for Dates Datetime Format Syntax
You can directly print the date object in any custom date format by using inbuilt i18nFormat function.
$frozenDateObj->i18nFormat('dd-MMM-yyyy');
Use datetime-syntax reference for more customization

PHP Date Interval issue

Hi I am trying to add 90 seconds to a date stored in my DB and compare it with my current time and then take some decision. I have written following code. Also sharing response i am getting.
$curr_date_time=new DateTime('now');
$start_date_time=new DateTime($judge_obj->created_at);
$finish_date_time=$start_date_time->add(new DateInterval('PT90S'));
print_r($curr_date_time);
print_r($start_date_time);
print_r($finish_date_time);
Response:
DateTime Object ( [date] => 2014-08-11 11:40:53 [timezone_type] => 3 [timezone] => UTC )
DateTime Object ( [date] => 2014-07-25 09:43:10 [timezone_type] => 3 [timezone] => UTC )
DateTime Object ( [date] => 2014-07-25 09:43:10 [timezone_type] => 3 [timezone] => UTC )
you see there is no difference in start_date_time and finish_date_time. please help
The add() method modifies the DateTime object itself ("by reference" if you will), so calling
$start_date_time->add(new DateInterval('PT90S'));
will actually update $start_date_time
Use
$start_date_time = new DateTime($judge_obj->created_at);
$finish_date_time = clone $start_date_time;
$finish_date_time->add(new DateInterval('PT90S'));

PHP simulate Day Light Savings

I have some code that handles dates and times. I think it's OK, but want to test that it works when the clocks go forwards for DST.
I can change the timezone using date_default_timezone_set('Europe/London');, and wondered if there is an easy to simulate being in DST without having to wait till the clocks change!
You don't provide details on what you want to test but I assume you have functions to do time-related stuff, e.g.:
function setExpiryTime(DateTime $start, $minutes){
}
The obvious test is to provide input parameters that will make your code cross DST boundaries. You can either find such information in your favourite search engine or run a simple PHP snippet:
<?php
$timezone = new DateTimeZone('Europe/London');
print_r( $timezone->getTransitions(mktime(0, 0, 0, 1, 1, date('Y')), mktime(0, 0, 0, 12, 31, date('Y'))) );
Array
(
[0] => Array
(
[ts] => 1388530800
[time] => 2013-12-31T23:00:00+0000
[offset] => 0
[isdst] =>
[abbr] => GMT
)
[1] => Array
(
[ts] => 1396141200
[time] => 2014-03-30T01:00:00+0000
[offset] => 3600
[isdst] => 1
[abbr] => BST
)
[2] => Array
(
[ts] => 1414285200
[time] => 2014-10-26T01:00:00+0000
[offset] => 0
[isdst] =>
[abbr] => GMT
)
)
Thus you can test:
setExpiryTime(new DateTime('2014-03-30T00:55:00+0000'), 10);
setExpiryTime(new DateTime('2014-10-26T00:55:00+0000'), 10);
you can certainly find out, whether user is in Daylight saving zone or daylight saving is currently in effect.
echo date('I', time());
This returns 0/1; where,
0 = Daylight saving is not in effect.
1 = Daylight saving is in effect.
more details : http://php.net/manual/en/function.date.php
Based on this result you can make conditional code to simulate your output.

Categories