Ninjakreborn Posted April 25, 2007 Share Posted April 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/ Share on other sites More sharing options...
paul2463 Posted April 25, 2007 Share Posted April 25, 2007 I have allways found strtotime() best for me $now = strtotime("Now"); $tomorrow = strtotime("+1 day", $now); $yesterday = strtotime("-1 day", $now); $nextMonth = strtotime("+1 month", $now); Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238076 Share on other sites More sharing options...
xenophobia Posted April 25, 2007 Share Posted April 25, 2007 Use "time()" to get the current time in seconds. Is a timestamp. So with seconds, you can easy do your calculation on it. Hope this help you. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238077 Share on other sites More sharing options...
obsidian Posted April 25, 2007 Share Posted April 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238084 Share on other sites More sharing options...
Ninjakreborn Posted April 25, 2007 Author Share Posted April 25, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238087 Share on other sites More sharing options...
taith Posted April 25, 2007 Share Posted April 25, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238088 Share on other sites More sharing options...
Ninjakreborn Posted April 25, 2007 Author Share Posted April 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238097 Share on other sites More sharing options...
obsidian Posted April 25, 2007 Share Posted April 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238099 Share on other sites More sharing options...
obsidian Posted April 25, 2007 Share Posted April 25, 2007 In further explanation to busi's last comment, you can use many of the same calculations within your MySQL query when you select DATETIME values: SELECT * FROM table WHERE myDate < (myDate + INTERVAL 1 WEEK); Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238102 Share on other sites More sharing options...
Ninjakreborn Posted April 25, 2007 Author Share Posted April 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238112 Share on other sites More sharing options...
paul2463 Posted April 25, 2007 Share Posted April 25, 2007 the arguments it takes all come from the date format ( day month year ) etc Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238114 Share on other sites More sharing options...
obsidian Posted April 25, 2007 Share Posted April 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238121 Share on other sites More sharing options...
Ninjakreborn Posted April 25, 2007 Author Share Posted April 25, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238133 Share on other sites More sharing options...
obsidian Posted April 25, 2007 Share Posted April 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238162 Share on other sites More sharing options...
steelmanronald06 Posted April 25, 2007 Share Posted April 25, 2007 I've always used Unix timestamps. They are always dependable and easy to work with. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238289 Share on other sites More sharing options...
taith Posted April 25, 2007 Share Posted April 25, 2007 i agree... they are quite simple... not always the best for sorting... but they work nicely for output... Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238292 Share on other sites More sharing options...
obsidian Posted April 25, 2007 Share Posted April 25, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238317 Share on other sites More sharing options...
taith Posted April 25, 2007 Share Posted April 25, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238340 Share on other sites More sharing options...
Ninjakreborn Posted April 25, 2007 Author Share Posted April 25, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238368 Share on other sites More sharing options...
AndyB Posted April 26, 2007 Share Posted April 26, 2007 That is assuming that the date format in mysql will also contain the second, minute, hour, and day. No. Unsurprisingly, DATE stores the date. If you want date and time - use the DATETIME format. Quote Link to comment https://forums.phpfreaks.com/topic/48623-solved-dates/#findComment-238687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.