Adding date() to date() unexptected ressults

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Stobor
Forum Newbie
Posts: 3
Joined: Tue Sep 24, 2013 5:21 am

Adding date() to date() unexptected ressults

Post by Stobor »

Im getting 2 date variables . One is $dowrow["FAILTIME"] (pulling it out from mysql) the other is currentlFailingLength



Code: Select all

$currentFailLength = date('H:i:s', (strtotime(date('H:i:s')) - strtotime($dowrow["LASTFAIL"])));

gives 02:53:56

$dowrow["FAILTIME"] is 00:00:04 and its TIME type in Mysql



Code: Select all

$totalFailLength = date('H:i:s', strtotime($dowrow["FAILTIME"]) + strtotime($currentFailLength));
echo $dowrow["FAILTIME"] ."+". $currentFailLength ."=". $totalFailLength;


shows 00:00:04+02:53:56=17:25:44

how to i get a proper answer in this example total should be 02:54:56 , right .... ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Adding date() to date() unexptected ressults

Post by Celauran »

Stobor
Forum Newbie
Posts: 3
Joined: Tue Sep 24, 2013 5:21 am

Re: Adding date() to date() unexptected ressults

Post by Stobor »

Well this got me further, but i still cant make it do what i want, if i dont type something like ('1 hour') instead of ($currentFailLength) $fail remains the same

Code: Select all

$fail = $dowrow["FAILTIME"];
$fail = new DateTime($fail);
date_add($fail, date_interval_create_from_date_string($currentFailLength));
echo date_format($fail, 'H:i:s');
gettype($currentFailLength) shows that its a string but it doesnt add anything

echo $currentFailLength shows 03:42:42

will exploding and turrning it in to seconds help ? is there a better way ?
Stobor
Forum Newbie
Posts: 3
Joined: Tue Sep 24, 2013 5:21 am

Re: Adding date() to date() unexptected ressults

Post by Stobor »

Code: Select all

list($hours,$minutes,$seconds) = explode(":", $currentFailLength);
$i = new DateInterval('PT'.$hours.'H'.$minutes.'M'.$seconds.'S');

$fail = $dowrow["FAILTIME"];
$fail = new DateTime($fail);
				
date_add($fail, $i);
fixed it somehow
Post Reply