I am writing a small script to analyse traffic to my website and am trying to calculate how many visits my site has had. I am looping through each unique ipaddress and want to know how many visits I have had from it. So a new visit is added if the last was more than 30 minutes ago. I have included my code below, it seems that $lastaccessed is always blank. Hope you can help. $a=0; while ($a < $num) { $Query = "SELECT * from analytics WHERE ipaddress = '" . $ipaddress[$a] . "'"; $Result = mysql_query( $Query ); $num2 = mysql_num_rows($Result); $b = 0; $lastaccessed = ''; while ($b < $num2) { $accessed = mysql_result($Result,$b,"access_time"); if ($lastaccessed = '') { $lastaccessed = $accessed; $visitcount++; } else { $temptime = ; //help here!! last access time + 30 mins if($accessed > $temptime) { $visitcount++; } else {} } $b++; } $a++; } PHP:
To compare DATETIME in PHP, you need to convert it to a UNIX timestamp format ( which would obviously allow you to add 3600 and get the +30min. effect ).
@nodlemon How can I do this? Does the rest of my code look correct because I think $lastaccessed is always blank!
It's blank because you set it to be blank ( line #8 ). function convert_datetime($str) { list($date, $time) = explode(' ', $str); list($year, $month, $day) = explode('-', $date); list($hour, $minute, $second) = explode(':', $time); $timestamp = mktime($hour, $minute, $second, $month, $day, $year); return $timestamp; } PHP: Pass the DATETIME field contents as an argument and it'll return the same data in UNIX timestamp format.
This thread today http://forums.digitalpoint.com/showthread.php?t=1617004 discussed a similar thing. But this line is setting your value to blank: if ($lastaccessed = '') Code (markup): ...because there should be a double equals sign there. If you use a single equals, you are just setting $lastaccessed to '' every time I couldn't tell you the number of times I've done that! Seems to afflict people who came to PHP from other languages for some reason...
You could do this to add 30 min to the time using timestamps <?php $curTime = time(); $newTime = $curTime + (30 * 60); $curTimeDate = date("H:i:s", $curTime); $newTimeDate = date("H:i:s", $newTime); echo "The time is $curTimeDate in 30 minutes it will be $newTimeDate"; ?> PHP:
Ha, keep it simple... But his date/time may not be in UNIX format, I think that was why the colourful suggestions.