The minus sign (-) is not displayed when the value is negative in PHP

1

I have 2 dates in the code, and I have to calculate the remaining time, I've already done that, but when this value is negative (the date has already been passed), I still show the value without the (-) sign. That's a problem because I need to verify if this time has passed or not yet.

This is the code used:

// AQUÍ SE LE DA FORMATO A LA FECHA

function format_interval(DateInterval $interval) {
        $result = "";
        if ($interval->y) { $result .= $interval->format("%y years "); }
        if ($interval->m) { $result .= $interval->format("%m months "); }
        if ($interval->d) { $result .= $interval->format("%d days "); }
        if ($interval->h) { $result .= $interval->format("%h hours "); }
        if ($interval->i) { $result .= $interval->format("%i min. "); }
        if ($interval->s) { $result .= $interval->format("%s seconds "); }

        return $result;
    }

    $first_date = new DateTime("$endDate"); // PRIMERA FECHA
    $second_date = new DateTime("$curDate"); //SEGUNDA FECHA

    $difference = $first_date->diff($second_date); 
    
asked by ByBrayanYT - Tops y más 22.05.2018 в 17:20
source

4 answers

1

Actually the difference already tells you if it's negative or positive.

Look at this:

$endDate = "2018-05-21";
$curDate = "2018-05-22";

a print_r ($ difference) returns this:

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 1
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 1
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

However, if we change the variables so that $ endDate is greater than $ curDate

$endDate = "2018-05-23";
$curDate = "2018-05-22";

Return this:

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 1
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 1
    [days] => 1
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

The magic is in [Invert] = 1 or [Invert] = 0

    
answered by 22.05.2018 / 17:39
source
3

Just as you have your code, it is enough to indicate that you want to show the sign, example:

   function format_interval(DateInterval $interval) {
            $result = "";
            // Esta linea añade un menos (-) si la fecha es negativa
            $result .= $interval->format("%r");

            if ($interval->y) { $result .= $interval->format("%y years "); }
            if ($interval->m) { $result .= $interval->format("%m months "); }
            if ($interval->d) { $result .= $interval->format("%d days "); }
            if ($interval->h) { $result .= $interval->format("%h hours "); }
            if ($interval->i) { $result .= $interval->format("%i min. "); }
            if ($interval->s) { $result .= $interval->format("%s seconds "); }

            return $result;
        }

        $first_date = new DateTime("23-05-2018"); // PRIMERA FECHA
        $second_date = new DateTime("22-05-2018"); //SEGUNDA FECHA

        $difference = $first_date->diff($second_date);
        echo format_interval($difference);
        // -1 days 

You can get more information about the DateInterval::format formats here: link

For the case that interests us:

  

% R | - > Sign "-" when it is negative, "+" when it is positive

     

% r | - > Sign "-" when it is negative, empty when it is positive

    
answered by 22.05.2018 в 18:35
0

When you subtract two dates this rule applies:

$objA->diff($objB) == $objB - $objA

A possible solution would be this (example):

$hoy= new DateTime('2018/5/22');
$fechaVieja= new DateTime('1773/7/4');

$interval = $hoy->diff($fechaVieja);
echo $interval->format('%r%a') . "\n\n";
// -89441

$interval2 = $fechaVieja->diff($hoy);
echo $interval2->format('%r%a');
// 89441
    
answered by 22.05.2018 в 17:44
0

It is also interesting to note that, according to the documentation , as of PHP 5.2.2, the DateTime objects can be compared using the comparison operators.

<?php
$fecha1 = new DateTime("now");
$fecha2 = new DateTime("tomorrow");

var_dump($fecha1 == $fecha2);  // bool(false)
var_dump($fecha1 < $fecha2);   // bool(true)
var_dump($fecha1 > $fecha2);   // bool(false)
?>
    
answered by 22.05.2018 в 17:49