Jump to content

Recommended Posts

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 :)

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

 

 

 

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);

?>

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.