Jump to content

[SOLVED] dates


Ninjakreborn

Recommended Posts

What is the most RELIABLE way to do date calculations.

The system I am working on is going to require a LOT of date calculations.

Like getting a month from the given date, or 3 days from it, or 4 days from it, or comparing months.

I need to find a good way to do date calculations, any advice.

Link to comment
Share on other sites

If you're doing that much with dates, I would hope the dates are stored in a database or other medium that supports its own date calculations. PHP date calculation is limited by the UNIX timestamp, so your date range is only available from 1970 - 2038. I'd recommend you use the MySQL datetime comparison functions whenever feasible:

 

MySQL Manual: DATE/TIME Functions

Link to comment
Share on other sites

Sorting/date ranges are easy.  The calculations are now

$now = strtotime("Now");

 

$tomorrow = strtotime("+1 day", $now);

 

$yesterday = strtotime("-1 day", $now);

 

$nextMonth = strtotime("+1 month", $now);

That made a lot of sense, however I am just needing a list of the different arguments that first one can take.

Here -1 day +1 day I am guessing you can put in more than 1 day, probably the same for month.

I like the way that looks.  Easy to use, accurate, and simple.

I always run off unix timestamps, and I am just looking for a quick way to do calculations, sorting them's never been much of a problem, but doing lot's of calculations are.

Is there a comprehensive list somewhere of the other arguments that first one can take.

Link to comment
Share on other sites

Yes, I have a unix timestamp.

Do you have a list of those arguments (now, + 1 month) and all those other stuff you showed for strtotime I didn't see those listed anywhere on php.net.  Do you have a list or a reference page or something where you learned them at?

It uses the GNU Date Input formats:

http://www.gnu.org/software/tar/manual/html_node/tar_109.html

 

for date output... just put a timestamp, time(), into your database...

for daytoday sorting... i find the best as date(Y).date(z) #outputs 2007195 or whatnot...

Never, ever store the UNIX timestamp in a MySQL database unless absolutely necessary. When there is a built in DATETIME datatype that gives you so much more flexibility with comparison and calculation, you need to learn to take advantage of the tools that are there rather than just getting by with using the limitations of the UNIX timestamp.

Link to comment
Share on other sites

Never, ever store the UNIX timestamp in a MySQL database unless absolutely necessary. When there is a built in DATETIME datatype that gives you so much more flexibility with comparison and calculation, you need to learn to take advantage of the tools that are there rather than just getting by with using the limitations of the UNIX timestamp.

90% of the time I end up having to work with a date within other fields.

I have to pull out a bunch of fields, and 2-3 of them are dates that I have to do specific calculations with.  They sometimes need to be outputted in multiple formats, adn sometimes have different calculations done.

If i used the mysql standard date format would I still have this flexibility.

I was concerned awhile back about date's, because I just saved it in the database under varchar using whatever format I needed to accept, Ihad many problems so someone on here introduced me to timestamps I have been using them ever since much smoother.  I have never used mysql's built in date format, however so I am unsure what it does/is.  I know it's a specific way it formats the date's, but are they easier (based on the usage I explained above) easier to work with than unix timestamps.

Link to comment
Share on other sites

If i used the mysql standard date format would I still have this flexibility.

Most definitely. Did you look at the link I posted to the variety of built-in functions at your disposal?

 

I have never used mysql's built in date format, however so I am unsure what it does/is.  I know it's a specific way it formats the date's, but are they easier (based on the usage I explained above) easier to work with than unix timestamps.

If you take the time to learn the tool, MySQL calculations are actually more flexible in some respects than using the timestamp. You mention having to do multiple calculations on different formats: give us an example of something you may have to do, and we may be able to save you multiple lines of PHP code by doing the same thing in one or two queries. It's hard to give you examples that will be practical when we don't know what it is you're trying to accomplish.

Link to comment
Share on other sites

			  	
<?php
				// checks if form is coming from the right place, and is sset
				if (isset($_REQUEST['id'])) {
					$id = $_REQUEST['id'];
					$select = "SELECT * FROM transactions 
									WHERE id = '$id' 
									AND userid = '" . $_SESSION['member'] . "';";
					$query  = mysql_query($select);
					if ($row = mysql_fetch_array($query)) {
						?>
<table border="0" cellspacing="0" cellpadding="4" class="memAcctOverviewTbl">
<tr>
<td><a href="javascript:void(0);" onmouseover="return overlib('Field help text goes here');" onmouseout="return nd();"><img border="0" src="<?php echo $wwwpath; ?>common/images/button_question.gif" width="17" height="17" align="left" /></a> <strong> Transaction ID:</strong></td>
<td><?php echo $row['transactionid']; ?></td>
<td></td>
</tr>
<tr>
<td><a href="javascript:void(0);" onmouseover="return overlib('Field help text goes here');" onmouseout="return nd();"><img border="0" src="<?php echo $wwwpath; ?>common/images/button_question.gif" width="17" height="17" align="left" /></a> <strong> Transaction Type:</strong></td>
<?php
// this get's the type since it varies.
// based on the type we are going to need to show different things
if ($row['type'] == "Deposit") {
$type = "Deposit";
}elseif ($row['type'] == "Bill") {
// this includes the billers name if it's a bill
$selectbiller = "SELECT * FROM biller WHERE id = '" . $row['biller_or_account'] . "';";
$querybiller = mysql_query($selectbiller);
if ($rowbiller = mysql_fetch_array($querybiller)) {
	$type = $rowbiller['billersname'];
}
}elseif ($row['type'] == "Payment") {
$type = "Payment";
}
?>
<td><?php echo $type; ?></td>
<td></td>
</tr>
<tr>
<td><a href="javascript:void(0);" onmouseover="return overlib('Field help text goes here');" onmouseout="return nd();"><img border="0" src="<?php echo $wwwpath; ?>common/images/button_question.gif" width="17" height="17" align="left" /></a> <strong> Transaction Amount:</strong></td>
<td><?php echo number_format($row['amount'], 2); ?></td>
<td></td>
</tr>
<tr>
<td><a href="javascript:void(0);" onmouseover="return overlib('Field help text goes here');" onmouseout="return nd();"><img border="0" src="<?php echo $wwwpath; ?>common/images/button_question.gif" width="17" height="17" align="left" /></a> <strong> Transaction Date:</strong></td>
<td><?php echo date("m/d/Y", $row['submitted']); ?></td>
<td></td>
</tr>
<tr>
<td><a href="javascript:void(0);" onmouseover="return overlib('Field help text goes here');" onmouseout="return nd();"><img border="0" src="<?php echo $wwwpath; ?>common/images/button_question.gif" width="17" height="17" align="left" /></a> <strong> Transaction Status:</strong></td>
<td><?php echo $row['status']; ?></td>
<td></td>
</tr>

</table>		
<?php
// here we need to check if the status is pending so we can output when it's changing to posted.
if ($row['status'] == "Pending") {

// in order to get the date, we have to do calculations at this point based on the Type of 
// transaction that we are currently dealing with
// CALCULATIONS
if ($row['type'] == "Bill" || $row['type'] == "Payment") {
$newtime = strtotime("+2 days", $row['submitted']);

}elseif ($row['type'] == "Deposit") {
$newtime = strtotime("+3 days", $row['submitted']);
}

// END CALCULATIONS
?>				
<p>Due to its <strong>Pending</strong> status, you may cancel this transaction before <?php echo date("F j, Y, g:i a", $newtime); ?></p>					


					<?php
} // end check for pending						

					}




				}

			?>				

 

Here is what I was currently working on.

It's working code, and it all functions.

This is an example, because I have the date stuff but it's just one part of a whole view of information.

Link to comment
Share on other sites

If you're looking to get an extra value (newtime in your case) based on the value of one of your columns, you can just use a SQL case statement and do it all in your query:

SELECT *, (CASE
WHEN type = 'Bill' || type = 'Payment' THEN DATE_FORMAT('%M %e, %Y, %l:%i %p', NOW() + INTERVAL 2 DAY)
WHEN type = 'Deposit' THEN DATE_FORMAT('%M %e, %Y, %l:%i %p', NOW() + INTERVAL 3 DAY)
END) as newtime
FROM transactions

 

This CASE statement will actually return your formatted date for you, and you can simply skip all the PHP calculations and echo the time directly as the "newtime" column in your record.

 

Hope this helps some.

Link to comment
Share on other sites

I'm not arguing the dependability of the UNIX timestamp. Indeed, when working with generating calendars and other things that are not dependent upon a database, they are indeed the most reliable way to handle things, but when you have a tool as optimal and powerful as the MySQL DATE, TIME and DATETIME datatypes (along with their accompanying functions), why use anything else?

Link to comment
Share on other sites

true enough :-)

just saying... mysql date()like functions are great at what they do...

but if you want absolute specifics... timestamp's are the way to go...

for sorting/organizing data withing mysql... use mysql functions...

for output, timestamp's with php's date() are much easier...

Link to comment
Share on other sites

Well having to do both, I am starting to think I should use the format like obsidian suggests from the database by default.  Then I can do the sorting or whatever else from there.  Then I can change what I need to timestamps as the time comes.  That is assuming that the date format in mysql will also contain the second, minute, hour, and day.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.