Reputation: 6480
Let's say I have this piece of code running inside a loop where $fid
will receive a value at each round:
$result2 = mysql_query( "SELECT date_create FROM users WHERE id = $fid");
Now, date_create will be in the format e.g
2012-08-16 20:13:49
My question is, how do I check if that date is before 2012-08-31, like 2012-08-10 ?
My full code, just in case one asks about it.
$query = "(SELECT distinct fid FROM coin WHERE uid = 59 )";
$result = mysql_query($query);
while($rows = mysql_fetch_array($result))
{
$fid = $rows['fid'];
echo 'fid || '.$fid;
$result2 = mysql_query( "SELECT id, date_create FROM users WHERE id = $fid");
$rows2 = mysql_fetch_array($result2);
echo " || users id : " . $rows2['id'];
if( $rows2['date_create'] < "2012-8-31") // How do i get something like this?
{
echo " || date_create : " . $rows2['date_create'] ." True";
}
else
{
echo " || date_create : " . $rows2['date_create'] . "False";
}
l();
}
Result I get so far is something like:
fid || 1112 || users id : 1112 || date_create : 2012-08-16 20:13:49 false <--
fid || 1113 || users id : 1113 || date_create : 2012-08-16 20:14:11 false <--
fid || 1115 || users id : 1115 || date_create : 2012-08-16 20:14:21 false <--
fid || 1117 || users id : 1117 || date_create : 2012-08-16 20:14:38 false <--
fid || 1118 || users id : 1118 || date_create : 2012-08-16 20:15:03 false <--
fid || 1119 || users id : 1119 || date_create : 2012-08-16 20:15:10 false <--
n.b would appreciate if also you could provide a way to check time. This would be extra :D
Upvotes: 1
Views: 2089
Reputation: 10716
You can do it by converting the date/time string and compare it using strtotime(). Using that method, you can also adapt it to compare for seconds as well:
//this effectively checks to see that the time is greater than '2012-08-16 20:13:49'
//the total calculated time on the RHS of the >= operator is effectively '2012-08-16 20:13:49'
//hence, this will echo 'YES'
//this is of the form: hours, minutes, seconds
$time_difference = (44 * 60 * 60) + (13 * 60) + 49;
if (strtotime('2012-08-16 20:13:49') >= strtotime('2012-08-15') + $time_difference)
{
echo 'YEAH';
}
else
{
echo 'NO';
}
UPDATE:
From the PHP documentation on strtotime():
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.
For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.
Upvotes: 1
Reputation: 8059
Better is to use the following query:
SELECT id, date_create,unix_timestamp(date_create) as time_stamp FROM users WHERE id = $fid"
and compare time_stamp variable which is int
Upvotes: 1
Reputation: 80639
You can use something like this:
$result = mysql_query($query);
$chk = strtotime("2012-08-31");
while( $rows = mysql_fetch_array($result) ) {
$fid = $rows['fid'];
echo 'fid || '.$fid;
$result2 = mysql_query( "SELECT id, date_create FROM users WHERE id = $fid");
$rows2 = mysql_fetch_array($result2);
echo " || users id : " . $rows2['id'];
if( $chk - strtotime($rows2['date_create']) > 0) {
echo " || date_create : " . $rows2['date_create'] ." True";
}
else {
echo " || date_create : " . $rows2['date_create'] . "False";
}
l();
}
Documentation on php.net for strtotime()
.
Upvotes: 1