PDA

View Full Version : dates issues


esthera
04-24-2007, 07:36 AM
my current code is


$datepurchased=$resultup['datepurchased'];
$t_100=$datepurchased-360000;
if ($amountaccessed>1 || $datepurchased>$t_100){


but this wrong

what i need to know is if it already passed 3 days from the $datepurchased
(meaning if today is the 24 then it should return if the datepurchased is before the 21st.)

can you help me get this working...

Pennimus
04-24-2007, 09:58 AM
I get the distinct impression that the above isn't really all your code, but what you want to do is essentially quite simple...


// This would be pulled from your database, but I've put a value here just so you can see it working.
$datepurchased = 20;

// Retrieve todays date
$datetoday = date(j);

// Calculate the difference between the two
$difference = $datetoday - $datepurchased;

if ($difference > 3){
echo "Over 3";
}


This doesn't work if it's not the same month, but it's a start that can be worked from.

esthera
04-24-2007, 10:16 AM
no because if i do

$datepurchased=$resultup['datepurchased']; //value of this is 2007-04-21 16:26:37
// Retrieve todays date
$datetoday = date('j');

// Calculate the difference between the two
$difference = $datetoday - $datepurchased;

this returns -1983 - it should return the amount of days

Pennimus
04-24-2007, 02:32 PM
Well, this is the problem, your original post didn't show what $resultup was, or $amountaccessed for that matter.

but anyway...

<?php

$datepurchased = $resultup['datepurchased']; // Date retrieved from DB
$datepurchased = strtotime($datepurchased); // Convert to unix timestamp
$datetoday = date("Y-m-d H:i:s"); // Get todays date in same format
$datetoday = strtotime($datetoday); // Convert to unix timestamp

$difference = $datetoday - $datepurchased; //Work out difference

if ($difference > 259200){ //259200 is 3 days in seconds
echo "Over 3 days";
} else {
echo "less than 3 days";
}
?>

CFMaBiSmAd
04-24-2007, 03:02 PM
I recommend that you use the mysql date and time functions to do this in your query, instead of retrieving your data and using PHP code to figure out if the data matches.

For example, the mysql DATEDIFF(expr1,expr2) function could be used to find records where your date column is three or more days ago -
SELECT whatever FROM your_table WHERE DATEDIFF(CURDATE(),your_date_column) >= 3