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.
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
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
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
)
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'));
How do you get today's date, as a date object?
I'm trying to compute the difference between some start date and today. The following will not work, because getdate() returns an array and not a date object:
$today = getdate();
$start = date_create('06/20/2012');
$diff = date_diff($start, $today);
echo($today . '<br/>' . $start . '<br/>' . $diff);
Output:
Array ( [seconds] => 8 [minutes] => 1 [hours] => 16 [mday] => 11 [wday] => 1 [mon] => 6 [year] => 2012 [yday] => 162 [weekday] => Monday [month] => June [0] => 1339455668 )
DateTime Object ( [date] => 2012-06-20 00:00:00 [timezone_type] => 3 [timezone] => America/Los_Angeles )
new DateTime('now');
http://www.php.net/manual/en/datetime.construct.php
Comparing is easy:
$today = new DateTime('now');
$newYear = new DateTime('2012-01-01');
if ($today > $newYear) {
}
Op's edit
I just needed to call date_default_timezone_set, and then this code worked for me.
To get difference in days use this:
$today = new DateTime('today');
the time in this object eill be 00:00:00
If you want difference with hours minutes and seconds use this:
$now = new DateTime('now');
I ended up using the date_create constructor (no parameter) to get the current date.
$diff = date_diff(date_create('06/20/2012'), date_create());
print_r($diff);
Output:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [invert] => 1 [days] => 8 )
I have no idea why, but Mike B's answer (and any constructor I tried for DateTime) threw an error for me in PHP5 / IIS.