...

View Full Version : Date question in php?



ArcGames
06-21-2006, 05:47 PM
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!

guelphdad
06-21-2006, 06:48 PM
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.

ArcGames
06-21-2006, 07:04 PM
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?

guelphdad
06-23-2006, 08:03 PM
You would use something like this to check the dates in mysql:



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum