Difference in hours between days using Carbon

Asked

Viewed 955 times

3

By checking the difference in hours between today and tomorrow I get the value of 6 which refers to the amount of hours until 00:00 today;

$t = \Carbon\Carbon::tomorrow();
$n = \Carbon\Carbon::now();
$n->diffInHours($t); // 6

But when I try to check with dates in the past I get an even bigger number;

$y = \Carbon\Carbon::yesterday();
$n->diffInHours($n) // 41

Because the values have this difference and because the date of "yesterday" has positive difference greater than tomorrow?

  • did not work the answer?

  • @Virgilionovic sorry I missed the appointment.

1 answer

2


Let’s create a minimal example, to exemplify and show what happens:

In the first result that is the code print_r( \Carbon\Carbon::yesterday() );

Carbon\Carbon Object
(
    [date] => 2018-01-14 00:00:00.000000
    [timezone_type] => 3
    [timezone] => America/Sao_Paulo
)

and in its result that is the code: print_r( \Carbon\Carbon::now() );

Carbon\Carbon Object
(
    [date] => 2018-01-15 16:51:15.992829
    [timezone_type] => 3
    [timezone] => America/Sao_Paulo
)

the difference in hours of these two results by the code:

print_r( \Carbon\Carbon::now()->diffInHours( \Carbon\Carbon::yesterday() ) );

is the value of 40, because, the first result to arrive on the day 15 has 24 hours + 16 hours on Monday is the value of 40 hours difference.

On the other that the general code as minimum example:

print_r( \Carbon\Carbon::tomorrow() );
print_r( \Carbon\Carbon::now() );
print_r( \Carbon\Carbon::now()->diffInHours( \Carbon\Carbon::tomorrow() ) );

Exit:

Carbon\Carbon Object
(
    [date] => 2018-01-16 00:00:00.000000
    [timezone_type] => 3
    [timezone] => America/Sao_Paulo
)
Carbon\Carbon Object
(
    [date] => 2018-01-15 16:56:28.216548
    [timezone_type] => 3
    [timezone] => America/Sao_Paulo
)

has a difference of 7 hours because the day 15/01/2018 16:56:28.216548 to get to the day 16/01/2018 missing 7 o'clock.


To know if one date and time is greater than the other:

\Carbon\Carbon::setLocale('pt_BR');

$result = \Carbon\Carbon::create(2012, 9, 6, 0, 0, 0)
            ->gt( \Carbon\Carbon::create(2012, 9, 6, 0, 0, 0) );

var_dump($result); // saída bool(false)
  • If the results always picked up the "smaller to bigger" how do I know how much time has passed? Currently I have a time at microtime and I need to know if that date is on microtime whether or not it is over...

  • @RFL see, what you reported in the comment is something else however, example: microtime e preciso saber se essa data que esta nesse microtime já passou ou não has already passed where? in relation to what. In the report of your question it is explained what you even asked is quite visible the difference, but please try to put in your question what you need, and not what the code does only! stand by.

  • when I said "pass" I referred to the same time... with a microtime how can I know if the current date is already higher than the microtime date.

  • @RFL can be put as an example in your question? to be a reference!

  • @RFL the method to know if one date is larger than the other is gt the answer is edited, I asked according question using Carbon

Browser other questions tagged

You are not signed in. Login or sign up in order to post.