Inserting date into mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Ok,

I know how to echo, but the code does not work. I dont have a clue what to change there.

regards,
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ok, a little bit different than the sample I posted your situation is:

try:

Code: Select all

$row = mysql_fetch_array($sql); //get a row from our result set 
$dateone = $row['start_date']; 
$datetwo = $row['end_date']; 
$daysleft = ((strtotime($datetwo) - strtotime($dateone))/86400); 
echo $daysleft;
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Yes, that did it... thank you.

I also need to calculate the number of days left from today's date and the end_date

I now I need to implement NOW() however this will format my date like 2005-10-21 and not 01-10-2005

Any ideas?
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Hmm,,


Actually scrap that. I have the start date and end date there I would like to run a script when the number of days left reaches 7.

I think I know how to do this.....

I will let you know if I get stuck.

---- Much appreciated is your help! ----
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Burrito wrote: Created for a reason this field type was. Fit your bill exactly it will.
LOL! Funny as hell this response is. LO*****'L!
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

I need to create a variable for today's date in the format;

2005-11-16

Any ideas?

This is so that I can run a script which emails me with the number of days left.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

$today = date("Y-m-d");
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Thank you!
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

why not let MySQL do that dirty stuff. Keeps your PHP clean:

Code: Select all

SELECT DATEDIFF('2003-11-20','2004-12-31');
all in the manual: http://dev.mysql.com/doc/refman/4.1/en/ ... tions.html

search for DATEDIFF on that page
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Hi,

I wonder if you could help me with a for loop. Here is the code;

Code: Select all

for($i = 0; $i < $numofrows; $i++) {
    
$row = mysql_fetch_array($sql); //get a row from our result set 


 $today = date("Y-m-d"); // Set today's date as variable
 $notify = 7; // Set notify variable to 7
 $datetwo = $row['end_date']; //set dattwo variable to the field value from the query
 $daysleft = ((strtotime($datetwo) - strtotime($today))/86400); 
	
	echo "<TR bgcolor=\"efefef\" onmouseover=this.className=\"test2\" onmouseout=this.className=\"test\" class=\"test\" onclick=this.className=\"test3\" class=\"test3\">\n";    
	echo "<TD><a href='viewasset.php?varl=".$row['id']."' class=\"blue5\">".$row['id']."</a></font></TD><TD>".$row['name']."</TD><TD>".$row['start_date']."</TD><TD>".$row['end_date']."</TD><TD>".$daysleft."</TD>\n"; 
    echo "</TR>\n";
		}
	echo "</TABLE>\n";
I want to display the days left in my $daysleft value for the loop. It is not working yet.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

use a while loop to loop your results instead you should.

/me likes patrikG's solution better anyway 8O
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

patrikG wrote:why not let MySQL do that dirty stuff. Keeps your PHP clean:

Code: Select all

SELECT DATEDIFF('2003-11-20','2004-12-31');
all in the manual: http://dev.mysql.com/doc/refman/4.1/en/ ... tions.html

search for DATEDIFF on that page
DATEDIFF() was added in MySQL 4.1.1. - if your mysql is < 4.1.1 you're out of luck :)
Post Reply