Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-21-2006, 05:47 PM   PM User | #1
ArcGames
New to the CF scene

 
Join Date: Jun 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ArcGames is an unknown quantity at this point
Question Date question in php?

What I am trying to do is compare dates using php and a mysql database. A date taken from my mysql database and compare it with today's date, and if today's date is 14 days more then the date taken from the database, then for it to echo a response message on a webpage, or if it less then I need it to echo a certain response message on a webpage. Please tell me if this code will work, if not please help me fix it, please?




<?php

$CurDate = date("Y").date("d").date("m");

// Make a MySQL Connection
mysql_connect("localhost", "my_username", "my_password") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());

$SignupDate_A = mysql_query("SELECT pdate FROM my_table") or die(mysql_error());

$SignupDate_B = strtotime($SignupDate_A);
$SignupDate = $SignupDate_B + 14;

if ($CurDate <= $SignupDate)
{
echo "You are below the expiration date";
}
elseif ($CurDate > $SignupDate)
{
echo "The Date has expired";
}

?>



Thanks!

Last edited by ArcGames; 06-21-2006 at 07:26 PM..
ArcGames is offline   Reply With Quote
Old 06-21-2006, 06:48 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Don't bother doing the date calculations from php. Do them directly in mysql.
Look up the use of DATE_SUB and DATE_ADD to get what you want and only return the relevant data from the database rather than all data.

your query will be significantly faster if you do it that way.
guelphdad is offline   Reply With Quote
Old 06-21-2006, 07:04 PM   PM User | #3
ArcGames
New to the CF scene

 
Join Date: Jun 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ArcGames is an unknown quantity at this point
I need to use php as I am using it on a website and also I am fairly new to php and mysql, could you please just fix my code to have it do what I am wanting, please?
ArcGames is offline   Reply With Quote
Old 06-23-2006, 08:03 PM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
You would use something like this to check the dates in mysql:

Code:
SELECT pdate FROM my_table where adddate(pdate, interval 14 day) < now()
that says to add 14 days to each pdate and only return those that are older than that.

Note that your query above would return every row in your table since you have no where condition. are you trying to compare a pdate for one specific user? if so you would add a condition for that in the query above.
guelphdad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:37 PM.


Advertisement
Log in to turn off these ads.