Mysterious Stranger
Mysterious Stranger

Reputation: 35

Substraction of dates in PHP doesn't work

I have a litle problem with a substraction of dates in PHP, here is one of the many codes that I tried :

date_default_timezone_set('Europe/Paris');
$log_end = new DateTime(); // date from which I want to substract a period
$log_begin = $log_end->sub(new DateInterval('PT0H1M2S')); //I substract the period to the date to find the when the log begins.
$log_b = $log_begin->format("Y-m-d H:i:s");
$log_e = $log_end->format("Y-m-d H:i:s");
print_r($log_b);
print_r("\n");
print_r($log_e);

Here is what is printed :

2014-12-03 21:50:41
2014-12-03 21:50:41

As you can see, the dates are the same, whatever methods I used, the result was the same. So did I made a mistake somewhere or what I want do is impossible ?

Thank you in advance for taking the time to help me and answer me, let me know if you need informations.

Upvotes: 0

Views: 37

Answers (2)

Mark Baker
Mark Baker

Reputation: 212412

$log_begin = $log_end->sub(new DateInterval('PT0H1M2S')); 

Subtracts the dateinterval from $log_end (so $log_end is now 2014-12-03 21:50:41) and returns a new DateTime object with the same (2014-12-03 21:50:41) value that is stored in $log_begin

To prevent $log_end from being adjusted, clone it to $log_begin and then subtract the period from $log_begin

date_default_timezone_set('Europe/Paris');
$log_end = new DateTime(); // date from which I want to substract a period
$log_begin = clone $log_end;
$log_begin->sub(new DateInterval('PT0H1M2S')); //I substract the period to the date to find the when the log begins.
$log_b = $log_begin->format("Y-m-d H:i:s");
$log_e = $log_end->format("Y-m-d H:i:s");
print_r($log_b);
print_r("\n");
print_r($log_e);

Upvotes: 1

Verhaeren
Verhaeren

Reputation: 1661

I created once a PHP class to handle dates, like substract or adding days (and/or months and/or years). Is that what you need? Check it:

class Dates
{

    public function __construct() {}

    public function add_date($_date, $_years, $_months, $_days, $_spacer, $_leading_zeros = 1) {

        $values = explode($_spacer, $_date);

        for($i = 0;$i < 3;$i++) {$values[$i] = round($values[$i]);}

        $values[2] += $_days;

        if($values[2] > 31) {
            $aux = floor($values[2] / 31);
            while($values[2] > 31) {$values[2] -= 31;}
            $values[1] += $aux;
        }

        $values[1] += $_months;
        $values[0] += $_years;

        if($values[1] > 12) {
            $aux = floor($values[1] / 12);
            while($values[1] > 12) {$values[1] -= 12;}
            $values[0] += $aux;
        }

        $leapYear = (($values[0] % 4 == 0) && (($values[0] % 100 != 0) || ($values[0] % 400 == 0))) ? 29 : 28;
        $days = Array(0, 31, $leapYear, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

        if($values[2] > $days[$values[1]]) {
            $values[2] -= $days[$values[1]];
            $values[1]++;
        }

        if($_leading_zeros) {return leading_zeros($values[0].$_spacer.$values[1].$_spacer.$values[2], $_spacer);}
        else {return $values[0].$_spacer.$values[1].$_spacer.$values[2];}

    }

    public function subtract_date($_date, $_years, $_months, $_days, $_spacer, $_leading_zeros = 1) {

        $values=explode($_spacer, $_date);

        for($i = 0;$i < 3;$i++){$values[$i] = round($values[$i]);}

        $days = Array(0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

        $values[0] -= $_years;
        $values[1] -= $_months;

        while($values[1] < 1) {
            $values[1] += 12;
            $values[0]--;
        }

        $values[2] -= $_days;

        while($values[2] < 1) {     
            $values[1]--;
            if($values[1] == 0) {
                $values[1] = 12;
                $values[0]--;
            }
            if($values[1] == 2) {$days[2] = (($values[0] % 4 == 0) && (($values[0] % 100 != 0) || ($values[0] % 400 == 0))) ? 29 : 28;}
            $values[2] += $days[$values[1]];
        }

        if($_leading_zeros) {return leading_zeros($values[0].$_spacer.$values[1].$_spacer.$values[2], $_spacer);}
        else {return $values[0].$_spacer.$values[1].$_spacer.$values[2];}
    }

    public function leading_zeros($_date, $_spacer) {

        $_date = $_spacer.$_date.$_spacer;

        for($i = 1;$i < 10;$i++) {
            while(strstr($_date, $_spacer.$i.$_spacer)){$_date = str_replace($_spacer.$i.$_spacer, $_spacer.'0'.$i.$_spacer, $_date);}
        }

        $_date = substr($_date, 1);
        $_date = substr($_date, 0, -1);

        return $_date;

    }

} 

Upvotes: 1

Related Questions