PHP timestamp if statement

I have php place a “timestamp” inside of Mysql database. With this timestamp I want to create an if statement that says

If “timestamp” is 3 hours older than the current time then Highlight ______ in orange;

I have the orange part figured out I dont quite understand how to put the three hours older part in.

the format I have for the timestamps is “11/30/2011 10:50:49 pm”

Is it a mysql TIMESTAMP or PHP time() (UNIX_TIMESTAMP)?


// if mysql TIMESTAMP:
$time = strtotime('11/30/2011 10:50:49 pm');

// otherwise just assign $time to the UNIX_TIMESTAMP

$diff = now() - $time;

if ($diff > 10800) { // 3 hours in seconds
    // 3 hours old or older
}

Time stamps are PHP time()

If you are storing your time as an integer in your database like 1234567890 (seconds since the start of the epoch), then all you have to do is some simple math.

Lets say in your result set called $rows you have name, age, registered time (reg_time)


$limit = (time() - 10800);

foreach($rows as $row){

echo 'Name : '. $row['name'] .'<br />' ;
echo 'Age : '. $row['age'] .'<br />' ;

  if( $row['reg_time']) > $limit ){
    echo 'Registered: '. date('Y m d h:i', $row['reg_time']) .'<br />' ;
  }else{
    echo '<span class=highlight>Registered: '. date('Y m d h:i', $row['reg_time']) .'</span><br />' ;
  }

}

untested btw.

Whoops, just noticed a typo in this code. Fixed it.

so I am putting the timestamp in on a submit page saying

$current = date("n/j/Y h:i:s a", time());

$sqlts = "UPDATE formdata SET dtrcompleted='$current' where form_id IN ($com)"; 

so basically I have this issue, I have tried this


$current = date("n/j/Y h:i:s a", time());

If($row[dtrcompleted] > $current-10800) {echo "<tr><td bgcolor='orange'>$row[location].....";

but the problem i’m running into is it is marking everything in orange, I tried running a echo on $current-10800 and I received -10788 as my sum, with $current = 12/5/2011 02:46:53 pm
in my example above $row[dtrcompleted] = 12/5/2011 08:03:05 am

Don’t sell MySQL short. Timestamps are very useful, but they aren’t as human parsable as reading a MySQL datetime field.

So if you have a registration time on a table use a datetime field. If you want to work with a timestamp you can always retrieve the data that way

SELECT UNIX_TIMESTAMP(`registeredOn`) as `registryTimestamp`

The major advantage of using a datetime field is the MySQL date interval functions are available at query time. This query gets all users registered in the last day.

SELECT * FROM users WHERE `registeredOn` &gt; DATE_SUB(NOW(), INTERVAL 1 DAY)

Yeah, you can do that with a timestamp - but which is easier to read?

Also, through the use of aliases nothing stops you from getting other information about the cell.


SELECT `id`, 
  `name`, 
  `registeredOn`, 
  IF ( `registeredON` &gt; DATE_SUB(NOW(), INTERVAL 3 HOUR, 1, 0 ) AS `highlight`
FROM users 
WHERE `registeredOn` &gt; DATE_SUB(NOW(), INTERVAL 1 DAY)

Here we put the highlight flag logic into the query. Note that PHP still has to make the formatting choice on what highlight means.

Keep in mind that SQL will usually be faster than PHP. And once you’re used to it as a language certain types of query lookups are easier to read in SQL than in their PHP equivalents.

Thanks Cups/Scootstah, yet again, genius solution for my problem.

What I needed to do is change the date string back into an integer and show difference


$difference = strtotime("-3 hours");

elseif (strtotime($row[dtrcompleted]) < $difference) {$timestamp=strtotime($row[dtrcompleted]); echo "<tr>
<td class='c22' bgcolor='orange'><center> $timestamp > $difference</center></td>

Thanks to everyone else who helped me work through this.


$row[dtrcompleted]

// SB (should be)

$row['dtrcompleted'] ;)

This will avoid PHP throwing notices into your logfile (and you should ideally be developing with notices being shown - turn them off when you deploy).

Thanks for your acknowledgement, but to be honest Michael Morris really nails why you ought to be using native Mysql dates in your base data unless you have exacting requirements that say you shouldn’t.

You have to accept that there is always a need to juggle date formats – and that PHP is adequately tooled up to help you do this using strtotime() and the excellent DateTime class which builds upon it.