yandoo Posted July 5, 2009 Share Posted July 5, 2009 Hi there, I was hoping for a little help please... I want to calcuate the number of days between the current date and a date found in a field in my database table. So pefectly illustrated is with this Mysql code: SELECT DATEDIFF (CURDATE(), SowDate) AS intval FROM `vegeschedule` Using the above code works a charm in phpmyadmin but i realise the tricky bit is to do it with php. So far i have found a basic coutdown script that counts down from the current date to a manually inputted date....I need to to use the current date and a date in a field in my table. Heres the coutdown script: <?php // Define date format $dateFormat = "Y-m-d H:i:s"; $targetdateDisplay = date($dateFormat,$targetdate); $actualdateDisplay = date($dateFormat,$actualdate); $result =mysql_query($sql); $row = mysql_fetch_array($result); // Define your target date here $targetYear = $row['year']; $targetMonth = $row['month']; $targetDay = $row['days']; $targetHour = 12; $targetMinute = 00; $targetSecond = 00; // End target date definition $targetdate = mktime($targetHour,$targetMinute,$targetSecond,$targetMonth,$targetDay,$targetYear); $actualdate = time(); $secondsDiff = $targetdate - $actualdate; $remainingDay = floor($secondsDiff/60/60/24); $remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60); $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60); $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60)); // Define date format $dateFormat = "Y-m-d H:i:s"; $targetdateDisplay = date($dateFormat,$targetdate); $actualdateDisplay = date($dateFormat,$actualdate); ?> <br /> <font size="+1" color="#FF0000">Expires In: <?php echo "$remainingDay days";?> </font> </p> Please NOTE the target date hasn't been set yet..... I tried to add my php query to get the date value from the database (below) but just dont know what the hell to do now.... mysql_select_db($database_connect, $connect); $sql = "SELECT ''SowDate' FROM vegeschedule"; If anybody could please help me that would be great...I have spent nearly ALL day and night yesterday trying to get working.... Thank You Quote Link to comment https://forums.phpfreaks.com/topic/164847-solved-countdown-from-current-date-to-recordset-date/ Share on other sites More sharing options...
MadTechie Posted July 5, 2009 Share Posted July 5, 2009 You totally lost me, you have a MySQL query that works but you want to do it in PHP then pass it to MySQL without having the target date! what exactly are you trying to get ? Quote Link to comment https://forums.phpfreaks.com/topic/164847-solved-countdown-from-current-date-to-recordset-date/#findComment-869249 Share on other sites More sharing options...
yandoo Posted July 5, 2009 Author Share Posted July 5, 2009 Im trying to count how many days from the current date to a retrieved date stored in my database.... The mysql code works fine in phpmyadmin... (just couldnt get it to work in dreamweaver) A record of a vegetable which includes a "SowDate" field is stored on one table....I want to calculate the number of days from the current date to the SowDate...and display the results.... How do i do this please?? Thank You Quote Link to comment https://forums.phpfreaks.com/topic/164847-solved-countdown-from-current-date-to-recordset-date/#findComment-869254 Share on other sites More sharing options...
MadTechie Posted July 5, 2009 Share Posted July 5, 2009 Simple solution would be write it manually instead of using dreamweaver, <?php $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } //May want to add a WHERE here $sql = "SELECT DATEDIFF (CURDATE(), SowDate) AS intval FROM `vegeschedule` "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["intval"]."<br />\n"; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/164847-solved-countdown-from-current-date-to-recordset-date/#findComment-869258 Share on other sites More sharing options...
yandoo Posted July 5, 2009 Author Share Posted July 5, 2009 Thats brilliant thank you, i was stuck on that for ages! Thank again Quote Link to comment https://forums.phpfreaks.com/topic/164847-solved-countdown-from-current-date-to-recordset-date/#findComment-869264 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.