SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I need to get the functionality of UNIX_TIMESTAMP in to my php logic

    HI Guys,

    Sorry for the weird title!

    I have this bit of code:

    Code PHP:
    $limit_time = time() - 20; // 1 minute timeout
     
    $sql = "SELECT * FROM online WHERE UNIX_TIMESTAMP(timestamp) >= $limit_time";
    $query = mysql_query($sql);
    $no = mysql_num_rows($query);
     
    while($row = mysql_fetch_array($query)) {
    	$ticket = $row['ticket'];
    	if($ticket == '1') {
    	$on = '1';
    	}
    	else { $on = '0';
    	}

    But what i really need to say is this:

    Code PHP:
    $limit_time = time() - 20; // 1 minute timeout
     
    $sql = "SELECT * FROM online";
    $query = mysql_query($sql);
    $no = mysql_num_rows($query);
     
    while($row = mysql_fetch_array($query)) {
    	$ticket = $row['ticket'];
            $timestamp = $row['timestamp'];
    	if($ticket == '1') {
              if(UNIX_TIMESTAMP($timestamp) >= $limit_time
    	$on = '1';
    	}
    	else { $on = '0';
     
     
    	}

    But, of course, UNIX_TIMESTAMP is a sql function and i get an error. I want to select everything from the db and then make the distinction of time in the if.....i cant think how to do it. Any ideas?

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    You can also extract the timestamp as unix timestamp in the query.
    PHP Code:
    $sql "SELECT *, UNIX_TIMESTAMP(timestamp) as u_timestamp FROM 

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha, thanks guys.........ive gone with doing it in the query as thereddevil suggested......i dont know why i didnt think of thatas ive used as dozens of time before!

    So if i wanted to use strotime i'd simply do:

    PHP Code:
    $timestamp $row['timestamp'];
    $time strtotime($timestamp); 
    ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •