PHP Date Interval issue - php

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

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

Laravel always return UTC date

My Lumen Api Controller:
$date = Carbon::createFromTimestamp(1444839127);
print_r($date);
$date2 = Carbon::createFromTimestamp(1444839127, 'Europe/Stockholm')->setTimezone('Europe/Stockholm');
print_r($date2);
return [$date, $date2];
Output:
Carbon\Carbon Object
(
[date] => 2015-10-14 19:12:07.000000
[timezone_type] => 3
[timezone] => Europe/Moscow
)
Carbon\Carbon Object
(
[date] => 2015-10-14 18:12:07.000000
[timezone_type] => 3
[timezone] => Europe/Stockholm
)
["2015-10-14T16:12:07.000000Z","2015-10-14T16:12:07.000000Z"]
.env:
APP_TIMEZONE=Europe/Moscow
bootstrap/app.php:
date_default_timezone_set(env('APP_TIMEZONE', 'America/Sao_Paulo'));
Therefore, it seems that laravel print Carbon object correctly, but always returns a timestamp in the UTC format. Unfortunately i need it with timezone

Carbon\Carbon giving me issues

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.

php date_diff / timestamp off by 31 minutes

I've generated a timestamp using code:
date_default_timezone_set("UTC");
$timestamp = date_create("now")->getTimestamp();
echo "value=$timestamp";
I get a value:
1484800385
I [later] want to grab the difference between that value and the current time.
$timestamp = "1484800385";
echo " timestamp=$timestamp<hr>";
print_r (new DateTime("#$timestamp"));
echo "<hr>";
print_r (date_create("now",timezone_open("UTC")));
and this outputs dates - but the difference in the time is greater than what I am expecting - by half an hour. The timezones are also shown differently.
timestamp=1484800385
DateTime Object ( [date] => 2017-01-19 04:33:05.000000 [timezone_type] => 1 [timezone] => +00:00 )
DateTime Object ( [date] => 2017-01-19 05:08:32.000000 [timezone_type] => 3 [timezone] => UTC )
I tried another script. First I generated a timestamp:
date_default_timezone_set("Australia/Sydney");
echo date_create("now")->getTimestamp(); // prints 1484800977
I then copied and pasted the value shown looked at it a few seconds later, compared to the date now
date_default_timezone_set("Australia/Sydney");
$date1 = new DateTime();
$date1->setTimestamp($timestamp);
print_r($date1);
$date2 = date_create("now");
print_r($date2);
and it's still wrong - by 31 minutes.
DateTime Object ( [date] => 2017-01-19 15:42:57.000000 [timezone_type] => 3 [timezone] => Australia/Sydney )
DateTime Object ( [date] => 2017-01-19 16:22:25.000000 [timezone_type] => 3 [timezone] => Australia/Sydney )
and UTC timezone
$timestamp = 1484801882;
date_default_timezone_set('UTC');
$date1 = new DateTime();
$date1->setTimestamp($timestamp);
$date2 = new DateTime();
print_r($date1);
print_r($date2);
results in the same timezone, but the current date is 31 minutes ahead of where I expect it to be.
DateTime Object ( [date] => 2017-01-19 04:58:02.000000 [timezone_type] => 3 [timezone] => UTC )
DateTime Object ( [date] => 2017-01-19 05:33:23.000000 [timezone_type] => 3 [timezone] => UTC )
What's going on?
Try to setTimeZone to UTC before print
$UTC = new DateTimeZone("UTC");
$date = new DateTime("now");
$date->setTimezone( $UTC );
print_r ($date);
echo "<hr>";
print_r (date_create("now",timezone_open("UTC")));
The output is
DateTime Object
(
[date] => 2017-01-19 05:17:52.000000
[timezone_type] => 3
[timezone] => UTC
)
>DateTime Object
(
[date] => 2017-01-19 05:17:52.000000
[timezone_type] => 3
[timezone] => UTC
)

Categories