Jump to content

Timestamp manipulation


nerd99

Recommended Posts

Hi,

 

I am quite new to the world of php so please excuse my ignorance in advance!

 

I am creating an invoicing system to use within my site, i.e. I create the invoice, it is then displayed on a users page. My issue is regarding retrieving a TIMESTAMP from a database and manipulating it. I have successfully created the invoice and sent all the data to a table in a database - including a timestamp in a column called 'stamp'. I want to be able to manipulate the timestamp to create a due date for my invoices and therefore be able to categorize them as outstanding or overdue.

 

Here is the page I am currently working with to view a due date...

<?php include('includes/admin_session.php'); ?>
<?php

        //Log page

        require_once("includes/connection.php");

        $ip   = $_SERVER['REMOTE_ADDR']; // Get ip
        $user = $_SESSION['username']; // Get ip
        $page = "Put page name here"; // Page name
        $b = time (); // Get time from server
        $timezone = (date_default_timezone_set("Australia/Sydney")); // Make time Sydney time.
$time = date ("l dS \of F Y h:i A"); // Format the date

        mysqli_query($connection,"INSERT INTO log (ip, username, page, time)
        VALUES ('$ip', '$user', '$page', '$time')") or trigger_error("SQL", E_USER_ERROR);

        // End Log Page
        ?>

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
        <head>
        <title>Admin Control Panel</title>
        <link href="style.css" rel="stylesheet" type="text/css">
        </head>
        <body>

        <!-- Inschool Logo and Horizontal Line image -->
           <div id="logo"></div>

<?php include('admin_links.php'); ?>   


        <!-- Main Body -->
           <div id="cp_bg">
           <div id=cp_body>
              <?php require_once("includes/connection.php"); ?>
   <div id="loggedin">logged in as '<?php echo $_SESSION['username'] ?>'</div>
<whitehead>Outstanding Accounts</whitehead><p></p>


<p></p>

<table border="0" width="550" cellpadding="0" align="left" class="stat">

<tr bgcolor='#881727'><td><whitehead>OUTSTANDING</whitehead></td></tr>
<tr bgcolor='#0066CC'><td><whitehead>Name</whitehead></td><td><whitehead>Invoice #</whitehead></td><td><whitehead>Invoice Period</whitehead></td><td><whitehead>Due Date</whitehead></td><td><whitehead>Amount Due</whitehead></td><td><whitehead>Record Payment</whitehead></td></tr>
<?php
require_once("includes/connection.php");

        $result = mysqli_query($connection,"SELECT * FROM invoices WHERE paid='0' ORDER BY id ASC")
        or die(mysqli_error($connection));

        while($row = mysqli_fetch_array($result)) {

        // Get all rows and assign them to variables

        $id = $row['id'];
        $bill_to = $row['bill_to'];
        $term = $row['term'];
        $year = $row['year'];
        $date = $row['date'];
        $paid = $row['paid'];
        $total = $row['total'];
        $stamp = $row['stamp'];
        $method = $row['method'];
        $invoice_period = $row['invoice_period'];

echo "<tr><td><a href='admin_view_outstanding_accounts2.php?invoice=".$id."' class='white'>".$bill_to."</a></td><td><a href='admin_view_outstanding_accounts2.php?invoice=".$id."' class='white'>".$id."</a></td><td><a href='admin_view_outstanding_accounts2.php?invoice=".$id."' class='white'>Term ".$term.", ".$year."</a></td><td><a href='admin_view_outstanding_accounts2.php?invoice=".$id."' class='white'>".$stamp."</a></td><td><a href='admin_view_outstanding_accounts2.php?invoice=".$invoice_num."' class='white'>$".$total.".00</a></td><td><a href='admin_view_outstanding_accounts2.php?invoice=".$invoice_num."' class='white'>Record Payment</a></td></tr>";

}

?>
</table>

                </div></div>
        </body>
        </html>

 

 

As it stands, you can see that I am just echoing out the data for $stamp. How do I do this?!!

 

Link to comment
https://forums.phpfreaks.com/topic/201139-timestamp-manipulation/
Share on other sites

You should always make any column that holds a date be of type DATE and store the date as YYYY-MM-DD. Datetime columns should be of type DATETIME and be stored as YYYY-MM-DD HH:MM:SS. Then you can format the information when you display it.

 

Ken

Ok. That is done and working, my column 'stamp' is now set as a DATE column and in the format you mentioned. I am able to send the date to the database then view it on another page (list of invoices).

 

How do I create a due date, say for example, 2 weeks from my DATE recorded when processing the invoice?

I have gained some ground with this but am now stuck with doing date comparisions.

 

I have two date variables:

$time - the current date

$due = strtotime('+2 weeks', strtotime($stamp)); <--- gives me a due date 2 weeks from creating the invoice. This is being calculated from my table field 'stamp' as mentioned in the above post.

 

How do I show the number of rows that have past the due date?

Here is a function that adds or subtracts years, month, weeks or days from a given date. It is HEAVILY commented (64 line of which on 3 are the function itself.

<?php
########################################
#	A function for adding or subtracting
#	years, months, weeks or days
#	from a given date
#
# there are three parameters
#		1.	$periods_type  - this is the word years, months, weeks or days
#		2.	$periods_qty - this is how many years, months, weeks or days
#												this can be either positve (in the future from the given date)
#												or negative (before the given date)
#		3.	$givendate -	this is the given date in YYYY-MM-DD format)
#
#	It returns a date in the YYYY-MM-DD format
#########################################

function calcDate($periods_type, $periods_qty,$givendate) {
    return date('Y-m-d', strtotime("$givendate $periods_qty $periods_type"));
}

######################################################
#
#		everything beyond here is simply a couple 
#		examples using the function
#
######################################################
echo "The given date is January 13, 2010 expressed as 2010-1-13. ";
echo "We will subtract 46 years from that date <br><br>";

$periods_type = "years";
$periods_qty = "-64";
$givendate = "2010-01-13";

$what_date = calcDate($periods_type,$periods_qty,$givendate);

if($periods_qty>0) {
  $before_after = "after";
}else{
  $before_after = "before";
  $periods_qty = $periods_qty * (-1);
}

echo $what_date . " is " . $periods_qty . " " . $periods_type . " " . $before_after . " " . $givendate;
echo "<hr>";
echo "The given date is today's date expressed as " . date("Y-m-d") . ". ";
echo "We will add 64 days to that date<br><br>";

$periods_type = "days";
$periods_qty = "64";
$givendate = date("Y-m-d");

$what_date = calcDate($periods_type,$periods_qty,$givendate);

if($periods_qty>0) {
  $before_after = "after";
}else{
  $before_after = "before";
  $periods_qty = $periods_qty * (-1);
}

echo $what_date . " is " . $periods_qty . " " . $periods_type . " " . $before_after . " " . $givendate;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.