php date_diff / timestamp off by 31 minutes - php

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
)

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

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 MongoDate toDateTime default timezone

When I create new DateTime object, it has timezone from "date.timezone" setting:
print_r(new DateTime());
DateTime Object
(
[date] => 2015-03-02 03:19:50.000000
[timezone_type] => 3
[timezone] => Europe/Moscow
)
But when I get DateTime object from MongoDate, it has UTC timezone:
print_r((new MongoDate()) -> toDateTime())
DateTime Object
(
[date] => 2015-03-02 00:19:50.000000
[timezone_type] => 1
[timezone] => +00:00
)
How can I setup MongoDate to create DateTime objects with my default timezone?
A way from do this:
$mongoDate = new \MongoDate();
$dateTimeDefaultTimeZone = $mongoDate->toDateTime()->setTimezone(new \DateTimeZone(date_default_timezone_get()))
You will can get the DateTime with default TimeZone.

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

Categories