#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0

    Adding to a time stored in database


    Ok, I have a date and time stored in a database using the following code I have 2 problems i need a little help with:

    PHP Code:
                                                                         
    <table border="1" width="50%" cellpadding="1" cellspacing="1">                                                            
                                                 
    <?php
    $username
    ="sip_SiPEcom";
    $password="nioSP61345";
    $database="sip_SiPEcom";
     
    mysql_connect(localhost,$username,$password);
    @
    mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM orders, customers AS c INNER JOIN customers_to_extra_fields AS e ON c.customers_id = e.customers_id";
     
     
    $result=mysql_query($query);
     
    mysql_close();
     
    echo 
    "<b><center>Database Output</center></b><br><br>";
     
    $resultarray = array();
    while (
    $row mysql_fetch_array($resultMYSQL_ASSOC)) {
        
    $rowarray = array();
        foreach (
    $row as $key => $value) {
            
    $rowarray[$key] = $value;
        }
        
    $resultarray[] = $rowarray;
    }

    $id 0;
    foreach (
    $resultarray as $row) {
     if(
    $row['customers_id'] != $id){
     echo 
    "<tr>";
      echo 
    "<td><b>".$row["customers_firstname"]."</b></td>";
      
      echo 
    "<td><b>".$row["customers_lastname"]."</b><br></td>";
      
      echo 
    "<td>".$row["customers_telephone"]."</td>";
       echo 
    "<td>".$row["date_purchased"]."</td>";
     }
     echo 
    "<td>".$row["value"]."";
     
    $id $row['customers_id'];

    }
     
    ?>
    PROBLEM 1.

    My date and time is stored as :

    2012-12-06 10:01:46

    this seems to output for users that have not made purchases and i would prefer the date to be in D / M / Y - H:M:S format.

    PROBLEM 2

    I want to use a selection of variables to contoll the adding of time IE

    1 hour
    2 hours
    3 hours
    4 hours
    5 hours
    6 hours
    ECT

    using this detail which will be stored in the database to show how many hours the customer has purchased will then add to the time purchased.

    eg on a customer that has made a purchase at 10am for 2 hours i want to output an expiry time of 12pm.

    not sure however how i would do this in php.

    any help appriciated.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    If I understand it correctly, you want to know how many hours someone bought. You'll need to add a second time in the database with that information.

    The simple solution would be to save the times as
    PHP Code:
    time(); 
    in the database, and lets say you buy 2 hours, you can save that as
    PHP Code:
     $date_purchased + ( 60 60 ); 
    Then you can convert the times easily to any format you want using
    PHP Code:
    date$format$time ); 
    More info on date formats here

    Hope it helps.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0
    Hi Timmeyy,

    Is it possible to do it without adding a time to the database, rather a text string to which I can assign a time frame?

    Originally Posted by Timmeyy
    If I understand it correctly, you want to know how many hours someone bought. You'll need to add a second time in the database with that information.

    The simple solution would be to save the times as
    PHP Code:
    time(); 
    in the database, and lets say you buy 2 hours, you can save that as
    PHP Code:
     $date_purchased + ( 60 60 ); 
    Then you can convert the times easily to any format you want using
    PHP Code:
    date$format$time ); 
    More info on date formats here

    Hope it helps.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    PHP Code:
    $time strtotime("2012-12-06 10:01:46"); // your starting time from string
    $time strtotime("+2 hours"$time); // plus 2 hours
    $format "Your format here"
    echo 
    date$format$time ); 
    But you'll have to store the "purchases" somewhere or the data is lost when you reload the page.

    Edit: If you dont need to store your starting time you can just put the new time into the database as a string
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0
    the starting time will always be the time stored in the data base, the expiry time is the date i want to output with 2 hours plused on to the start time, the 2 hours may be 3 hours or 4 hours depending on customer purchase but will always be stored in the database as 1 hour. 2 hours ect
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0
    If you store the hours purchased as "2 hours", something like this will work:
    PHP Code:
    echo "<td>".$row["date_purchased"]."</td>"
    $time strtotime($row["date_purchased"]); 
    $time strtotime("+".$row["hours_purchased"], $time);
    echo 
    "<td>".date"Y-m-d H:i:s" ,$time)."</td>"
    I still highly recommend not to save your date/times like this.

IMN logo majestic logo threadwatch logo seochat tools logo