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
)
)
Related
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
I want to add 2 seconds for current date object, I am getting data objects as propel foreach array result, this is the object I receive
DateTime Object
(
[date] => 2020-05-22 09:03:21.000000
[timezone_type] => 3
[timezone] => Australia/Melbourne
)
I tried this ways but no any change,
$row->getStartTime()->add(DateInterval::createFromDateString('+2 seconds'));
$row->getStartTime()->add(new DateInterval('PT2S'));
But I am unable to get the new with 2 seconds added for received time.
$time = $row->getStartTime()->format('H:i:s');
$callTimeMax = date('H:i:s', strtotime('+3 seconds', strtotime($time)));
This is my approach, It worked.
hello i'm trying to build an application that plots a graph for the last 7 days beginning with today, and i'm using Laravel 5 - Carbon\Carbon date package..
so if today is wednesday it would end on tuesday
if today is friday it would end on thurdsay nxt week
$now = Carbon::now();
$ar = [];
for($i=1; $i<7; $i++){
array_push($ar, $now->subDays($i));
}
pr($ar, true);
where pr() is a helper function similar to dd() i wrote that just die-dumps in a simple way, but i get this output, with the first problem being that, keeping in mind that today is - Friday, 30th November, 2018, 2018-11-30
Array(
[0] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[1] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[2] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[3] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[4] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
[5] => Carbon\Carbon Object
(
[date] => 2018-11-09 12:23:53.793501
[timezone_type] => 3
[timezone] => UTC
)
it should continue as 29th, 28th, 27th, but its going far back as 09th of november, and its not even sequencial, like 09, 08, 07, 06, just 09 of november and i dont know what i'm getting wrong, please i need assistance, thanks.
When you use a function on a carbon object, the object will update itself and return a reference to the object. So, your $ar array contains several references to the same object. Every time you use subDays you are actually updating every object in your array.
If you want to fill your array with different carbon objects, you have to create a new instance every time. For example, like this:
$ar = [];
for($i=1; $i<7; $i++){
array_push($ar, now()->subDays($i));
}
now() is a helper function in Laravel that returns a new instance of Carbon::now().
$day = Carbon::today();
$ar = [];
for($i=1; $i<=7; $i++){
array_push($ar, $day);
$day->subDay(1);
}
Here $day->subDay(1); method is modifying the object value itself subtracting by 1 instead of returning the modified value.
$ar = [];
for($i=1; $i<7; $i++){
$now = Carbon::now();
$ar[] = $now->subDays($i);
}
Carbon will update it's instance everytime you call subdays, so you need to reninitilize.
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!
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'));