www.webdeveloper.com
Results 1 to 4 of 4

Thread: Adding date variable to date varriable

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    41

    Adding date variable to date varriable

    Im getting 2 date variables . One is $dowrow["FAILTIME"] (pulling it out from mysql) the other is currentlFailingLength
    Code:
    $currentFailLength = date('H:i:s', (strtotime(date('H:i:s')) - strtotime($dowrow["LASTFAIL"])));
    gives 02:53:56

    $dowrow["FAILTIME"] is 00:00:04

    Code:
    $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 .... ?

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    An important thing to remember: strtotime make a time stamp for the entire YEAR. so you are adding two full dates. Here, I suck at explaining things but here is a script with the full break down, maybe it will help you understand.
    PHP Code:
    <?php

    $dowrow
    ["FAILTIME"] = '00:00:04';
    $currentFailLength '02:53:56';

    $s1 strtotime($dowrow["FAILTIME"]);
    $s2 strtotime($currentFailLength);

    $totalFailLength date('H:i:s', (strtotime($dowrow["FAILTIME"]) + strtotime($currentFailLength)));
    echo 
    "Stamp 1: {$s1}\nStamp 2: {$s2}\n\n";
    echo 
    "Stamp 1 + Stamp 2 = " . ($s1+$s2) . "\n\n";
    echo 
    "Stamp 1 [Ymd His]: " date("Y-m-d H:i:s"$s1) . "\nStamp 2 [Ymd His]: " date("Y-m-d H:i:s") . "\n\n";
    echo 
    "Added Stamps [Ymd His]: " date("Y-m-d H:i:s", ($s1+$s2)) . "\n\n";
    echo 
    $dowrow["FAILTIME"] ." + "$currentFailLength ." = "$totalFailLength "\n\n";

    /*WHAT YOU NEED IS BELOW THIS LINE*/

    $correctWay date("H:i:s"strtotime("+".$currentFailLengthstrtotime($dowrow["FAILTIME"])));

    echo 
    "The result you want: {$correctWay}";

    ?>

  3. #3
    Join Date
    Jul 2012
    Posts
    41
    Thanks NoEffinWay but your option didnt work, fixed it myself though


    Code:
    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);
    
    echo date_format($fail, 'H:i:s');

  4. #4
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Yeah, I realized now that the time is wrong. I shouldn't be awake this early. Anyway, glad you figured it out, good luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles