Calcul d'un délai en + ou et en - par rapport à une date donnée

Fermé
alecour - 1 mai 2013 à 09:00
Bonjour,
Ci-dessous mon source en test :
<?php

function add_date($date,$day=0,$mth=0,$yr=0) {
$newdate = new DateTime($date);
$newdate = $newdate->add(date_interval_create_from_date_string($yr . ' year'));
$newdate = $newdate->add(date_interval_create_from_date_string($mth . ' month'));
$newdate = $newdate->add(date_interval_create_from_date_string($day . ' days'));
$r = $newdate->format('d.m.Y');
return $r;
}
function sub_date($date,$day=0,$mth=0,$yr=0) {
$newdate = new DateTime($date);
$newdate = $newdate->sub(date_interval_create_from_date_string($day . ' days'));
$newdate = $newdate->sub(date_interval_create_from_date_string($mth . ' month'));
$newdate = $newdate->sub(date_interval_create_from_date_string($yr . ' year'));
$r = $newdate->format('d.m.Y');
return $r;
}
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<title>Formulaire HTML5</title>
<style>
body{
background-image: #c1c1c1;
font-family: 'Open Sans',sans-serif;
font-weight: 400;
font-size: 13px;
}
.input {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
border:1px solid #ccc;
font-size:20px;
width:500px;
line-height:30px;
display:block;
margin-bottom:15px;
margin-top:5px;
text-align:center;
background:aqua;
outline: none;

-webkit-border-radius:5px;
-moz-border-radius:5px;
-o-border-radius:5px;
-ms-border-radius:5px;
border-radius:5px;
}
</style>
</head>
<body>
<div class="input">
<?php
$dated = date('21.03.1953');
$datef = date('12.12.1954');
$mo = 8;
$jr = 21;
$yr = 1;
$libel = '';
$lib2 = '';
$libel = add_date($dated,$jr,$mo,$yr);
$mo = 8;
$jr = 21;
$yr = 1;
$lib2 = sub_date($datef,$jr,$mo,$yr);
?>
<?= $libel.'<br>'.$lib2; ?>
</div>
</body>
</html>
Lorsque j'exécute cela, ça marche bien.
Ce que j'aimerais est d'avoir le même résultat dans une seule et même fonction
en ayant mélangé les paramètres en + et - !!!
Je n'obtiens pas le même résultat dans une seule fonction, exemple:
$mo = -8;
$jr = -21;
$yr = -1;
$lib2 = add_date($datef,$jr,$mo,$yr);
$lib2 = '22.03.1953' alors que j'attends '21.03.1953'
alors que :
$mo = 8;
$jr = 21;
$yr = 1;
$lib2 = sub_date($datef,$jr,$mo,$yr); me donne bien '21.03.1953'
Comment puis-je arriver à le faire.
Merci à tous