1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Adding 30 mins to a datetime

Discussion in 'PHP' started by adzeds, Dec 17, 2009.

  1. #1
    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:

     
    adzeds, Dec 17, 2009 IP
  2. NodLemon

    NodLemon Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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, Dec 17, 2009 IP
  3. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #3
    @nodlemon
    How can I do this?

    Does the rest of my code look correct because I think $lastaccessed is always blank!
     
    adzeds, Dec 17, 2009 IP
  4. NodLemon

    NodLemon Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    NodLemon, Dec 17, 2009 IP
  5. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #5
    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...
     
    markowe, Dec 17, 2009 IP
  6. Affix

    Affix Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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:
     
    Affix, Dec 17, 2009 IP
  7. KEEP IT REAL

    KEEP IT REAL Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    LOL
    
    $accessed = time();
    $temptime = time() + 1800;
    PHP:
     
    Last edited: Dec 17, 2009
    KEEP IT REAL, Dec 17, 2009 IP
  8. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #8
    Ha, keep it simple... But his date/time may not be in UNIX format, I think that was why the colourful suggestions.
     
    markowe, Dec 18, 2009 IP
  9. adzeds

    adzeds Well-Known Member

    Messages:
    1,209
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #9
    Many Thanks.

    Problem sorted!
     
    adzeds, Dec 18, 2009 IP
  10. KEEP IT REAL

    KEEP IT REAL Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    then just add strtotime($whatever_format); :)
     
    KEEP IT REAL, Dec 18, 2009 IP