Jump to content

Datediff syntax


graham23s

Recommended Posts

Hi Guys,

 

i'm trying to use the datediff in mysql to work out the days from when a member registered untill thye logged in! i have:

 

 
<?php
$q_date_diff = "SELECT DATEDIFF (`registered`,`last_login`) WHERE `id`='$users_id'";
$r_date_diff = mysql_query($q_date_diff);
$num_days = mysql_num_rows($r_date_diff);
print("Has been a member ($num_days) days.");
?>

 

but this throws up an error i take it my syntax is off is that right?

 

thanks guys

 

Graham

Link to comment
Share on other sites

mysql_num_rows() count the number of rows in the result set.

<?php
$q_date_diff = "SELECT DATEDIFF (`registered`,`last_login`) AS num_days WHERE `id`='$users_id'";
$r_date_diff = mysql_query($q_date_diff);
$rs = mysql_fetch_array($r_date_diff);
print("Has been a member ({$rs["num_days"]}) days.");
?>

Link to comment
Share on other sites

Hi Mate,

 

there seems to be an error with my query i even did this in a simple state to test:

 

<?php
// date diff how many days... //
$q_date_diff = "SELECT DATEDIFF (`2008-03-16 04:53:45`,`2008-04-02 11:01:08`) AS num_days WHERE `id`='$users_id'";
$r_date_diff = mysql_query($q_date_diff);
$a_date_diff = mysql_fetch_array($r_date_diff);
$days = $a_date_diff['num_days'];

print("$q_date_diff");
print("Has been a member ($days) days.");
?>

 

i still get:

 

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

 

is it the syntaxt i have do you think?

 

cheers

 

Graham

Link to comment
Share on other sites

$q_date_diff = "SELECT DATEDIFF ('2008-03-16 04:53:45','2008-04-02 11:01:08') AS num_days WHERE `id`='$users_id'";

Use single qoutes around values not the back ticks. If you still get error, use mysql_error() to know error details.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.