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
}
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)
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` > 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` > DATE_SUB(NOW(), INTERVAL 3 HOUR, 1, 0 ) AS `highlight`
FROM users
WHERE `registeredOn` > 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.
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.