CalendarView get only today's events - php

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!

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

loop date insert x times php

I am trying to put together some code in PHP so that after I add a date, the next x number dates are added.
Without going into specifics, the general idea is that after a customer has been visited, the visit date will be entered, this then triggers this code, to add the next x visit dates in (the visits will be every 6 months), but depending on the customer there may be 3, 4 or 5 visits, I have this set as a $value and am good with that and being able to calculate future dates.
What I dont know is how to loop the insert, so if
$value = 4, then
n1, initial date + 6 months
n2, initial date + (6*2) months
n3, initial date + (6*3) months
n4, initial date + (6*4) months - yes i know i could just add 6 months to n-1, but trying to keep it simple.
I have never worked with nth term approach in a loop and wondered if someone could a) point me in the direction of a related article, and b) give me an example.
Basically what I want to do is LOOP it x times ($value = x), and then in each loop be able to use 6 * x.
Thanks
Using the DateTime object and the DateInterval Object this can be done quite simply using a for loop.
$frequency = 6; // could be 6 or 12 months, set elsewhere in your code
$visit_count = 5; //for this example
$initial_date = new DateTime($value["VisitDate"]); // last visit date
$interval = new DateInterval("P$frequencyM"); // visit every $frequency months
for ( $i=0; $i < $visit_count; $i++ ) {
$visits[] = clone $initial_date->add($interval);
}
print_r($visits);
RESULT
Array
(
[0] => DateTime Object
(
[date] => 2023-01-01 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[1] => DateTime Object
(
[date] => 2023-07-01 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[2] => DateTime Object
(
[date] => 2024-01-01 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[3] => DateTime Object
(
[date] => 2024-07-01 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[4] => DateTime Object
(
[date] => 2025-01-01 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
)

How to convert date/time in multilingual Cakephp 3 website

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

DateTime period issues

I've got strange behaviour with datetime's in php.
For some reason this code produces wrong result:
$period = new DatePeriod(
new DateTime('2017-03-20'),
DateInterval::createFromDateString('1 day'),
new DateTime('2017-03-31')
);
foreach($period as $dt){
$a[] = $dt->format('Y-m-d');
}
So expected result is period from 20 to 31, but it's not. Here is actual result:
Array
(
[0] => 2017-03-20
[1] => 2017-03-21
[2] => 2017-03-22
[3] => 2017-03-23
[4] => 2017-03-24
[5] => 2017-03-25
[6] => 2017-03-26
[7] => 2017-03-27
[8] => 2017-03-28
[9] => 2017-03-29
[10] => 2017-03-30
)
So what i'm missing here, or that's a php bug?
Following this user statement on official PhP documentation about DatePeriod::
[...] an example to include the end date using the DateTime method 'modify'
This class seems to ignore the end date. You will need to modify the end date to include this gap (of +1 unit, in your case +1 day).

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