please help limit unix_timestamp by last 7 days

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris666uk1
Forum Newbie
Posts: 2
Joined: Thu Nov 17, 2011 10:39 am

please help limit unix_timestamp by last 7 days

Post by chris666uk1 »

please could some one help me i need to limit the query by timestamp for the last 7 days here is my code on the php side

Code: Select all

 <?php
			 
             $db = @mysql_connect("localhost", "8conv", "*****") or die("Connection Error: " . mysql_error());
             mysql_select_db("tempmonitor") or die("Error connecting to db.");
             $sql = "SELECT *, UNIX_TIMESTAMP(datetime) AS datetime FROM 8conv";
             $result = mysql_query($sql);
             $data = array();
             while ($row = mysql_fetch_array($result)) {
                $temp1[] = array($row['datetime'] * 1000, (int)$row['temp1']);;
				
             }
            
			 $temp1 = json_encode($temp1);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: please help limit unix_timestamp by last 7 days

Post by Celauran »

Code: Select all

$dt = new DateTime("7 days ago");
$limit = $dt->format('Y-m-d H:i:s');
$query = "SELECT foo FROM bar WHERE datetime > {$limit}";
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: please help limit unix_timestamp by last 7 days

Post by pickle »

Better to do the date math in MySQL.

Add a WHERE clause like:

Code: Select all

WHERE
  `datetime` > DATE_SUB(NOW(), INTERVAL 7 DAY)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply