PHP Date Object Add 3 Seconds - php

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.

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

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

Get month from mssql DateTime Object with php

When I print the Array I get this
[CreatedDate] => DateTime Object ( [date] => 2013-03-20 00:00:00 [timezone_type] => 3 [timezone] => America/Denver )
I'm trying to pull the month from the date, so far no luck. Been doing a combination of the below code and got a date to return but it was 12/31/1969 which is not in my database
$month = date("m",($row['CreatedDate']));
$month = date("m",($row['CreatedDate.date']));
$month = date("m",($row['date']));
Whatever produced the DateTime object already has what you need. You just need to call format() to get the month:
echo $object->format('m');

get date from DateTime Object

After more than an hour struggling and trying I'd like to ask it here.
Trying to make something with weeks etc. in php I got from you site this:
Get all Work Days in a Week for a given date
Nice and will work for me fine.
But ... I can't get, trying and trying, the data out of this part: [date] => 2013-08-12 00:00:00
Array
(
[0] => DateTime Object
(
[date] => 2013-08-12 00:00:00
[timezone_type] => 3
[timezone] => Europe/Amsterdam
)
How to get that date out of the array ?
Please help me out, thanks in advance for the help !
Use DateTime::format()
$dateTime = new DateTime('2013-08-12 00:00:00');
echo $datetime->format('Y-m-d'); // produces 2013-08-12
$firstMondayThisWeek= new DateTime('2013-08-12');
$firstMondayThisWeek->modify('tomorrow');
$firstMondayThisWeek->modify('last Monday');
$nextFiveWeekDays = new DatePeriod(
$firstMondayThisWeek,
DateInterval::createFromDateString('+1 weekdays'),
4
);
$dateTimes = iterator_to_array($nextFiveWeekDays);
foreach ($dateTimes as $dateTime) {
echo $dateTime->format('Y-m-d H:i:s');
}

Categories