forumnz Posted September 23, 2007 Share Posted September 23, 2007 Hi I am creating a simple auction site. There will be a start date which I think will be a timestamp and an end date. How can I do code the end date so that it is $days until it closes? I am not asking for code (although that may be helpful) I am asking for an explanation. Thanks a lot, Sam. Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted September 23, 2007 Share Posted September 23, 2007 you take the start date and plus with $days. if you are going to use timestamp you should split it up in $day, $month and $year... then you just plus $days with $day $endDay = $day + $days; store $endDay in databasen. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 No need to split and store three extra fields. SELECT (TO_DAYS(enddate) - TO_DAYS(startdate)) as daysleft Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted September 23, 2007 Share Posted September 23, 2007 he doesnt have the enddate... he need to addon $days to startdate... Quote Link to comment Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 read it again... There will be a start date which I think will be a timestamp and an end date Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted September 23, 2007 Share Posted September 23, 2007 read the last part too.... How can I do code the end date so that it is $days until it closes? Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted September 23, 2007 Share Posted September 23, 2007 Just to be clear; Do you have the start data AND the end date, but want to display how many days are left in the Auction? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 If it the other way round, the comments about extra fields still hold true - use the datetime functions SELECT startdate, (startdate + INTERVAL $daysleft DAY) as enddate ... (This way resolves dates like 35th June for you) Quote Link to comment Share on other sites More sharing options...
forumnz Posted September 23, 2007 Author Share Posted September 23, 2007 Basically I want to find the easiest way to store a start and end date in a db. If the start date is something like 2007-10-10 22:13:46 then then end date should be (for a 7 day auction) 2007-10-17 22:13:46. I also need to be able to code it so that I can display the time left (e.g. 6h 18m). Im not asking for an explanation on that one, but the first problem must accomodate for that. So, sorry about the confusion, what is the best way now? Thanks very much, Sam. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 make it a DATE field in the db then use date(format here); to do the start and end date. Quote Link to comment Share on other sites More sharing options...
forumnz Posted September 23, 2007 Author Share Posted September 23, 2007 I just made this and it displays start date and end date (7days). Can you tell me if I coded it well please? Sam. <?php $d = date("d"); echo date('d m Y G:i:s'); $sd = date('$d m Y G:i:s'); echo "<br /><br />"; $dd = $sd + (7+$d); echo $dd . date(' m Y G:i:s'); ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 i guess $sd displays adifferent date format it should be $sd+7? would be the date format for sd plus seven days from the current sd date format. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 It would be better as DATETIME fields to include the h:m:s element too <?php $saleitem = 'widget extractor'; $duration = 7; $sql = "INSERT INTO auction (item, startdate, enddate) VALUES ('$saleitem', NOW(), NOW()+INTERVAL $duration DAY)" ?> For the time left, you need to compare the end datetime with the current datetime <?php $endtime = strtotime ($db_enddate); // enddate from the db record for the item $seconds_left = $endtime - time(); $days_left = floor($seconds_left / 86400); $seconds_left = $seconds_left % 86400; $hours_left = floor($seconds_left / 3600); $seconds_left = $seconds_left % 3600; $mins_left = floor($seconds_left / 60); $seconds_left = $seconds_left % 60; echo "$days_left days $hours_left hours $mins_left minutes $seconds_left seconds left"; ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 cool thanks barand that would be easier than making a huge uneeded function to do date and time LOL Quote Link to comment Share on other sites More sharing options...
forumnz Posted September 23, 2007 Author Share Posted September 23, 2007 Hi and thanks, this is what I have: <?php $duration = 7; //Connect to mysql server $link=mysql_connect("localhost","@@@",@@@"); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db=mysql_select_db("bbm@@@rs"); if(!$db) { die("Unable to select database"); } $qry="INSERT INTO auctions (start, end) VALUES (NOW(), NOW()+INTERVAL $duration DAY)"; ?> It hasn't inserted into db though. Am I doing something wrong. The db has start and end both as time stamps. The start one is a current one. Should they be different? Sam. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 23, 2007 Share Posted September 23, 2007 i noticed a slight syntax error try this: <?php $duration = 7; //Connect to mysql server $link=mysql_connect("localhost","@@@","@@@"); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db=mysql_select_db("bbm@@@rs"); if(!$db) { die("Unable to select database"); } $qry="INSERT INTO auctions (start, end) VALUES (NOW(), NOW()+INTERVAL $duration DAY)"; ?> Quote Link to comment Share on other sites More sharing options...
forumnz Posted September 23, 2007 Author Share Posted September 23, 2007 Hi Thanks I got it now! Sam. Quote Link to comment 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.