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