开发者

Working with dates in PHP, how many hours old is date/time stamp?

Ok, I am storing a Date/Time in a MySQL TEXT field. I want to be able to check if the date stored in the database is atleast 48 hours from the current date/time. Here is some code:

<?php
date_default_timezone_set("America/New_York");
$dtformat = "F jS, Y - g:i A";

$datetime = date($dtformat);
?>

The value of $datetime is sent to MySQL and placed in a TEXT column, I would use MySQL DATETIME, but the timezone needs to be different, depending on the users location. So I figured I would make my timestamp in PHP. An example output would be January 11th, 2011 - 6:24 PM

How would I take the current dat开发者_StackOverflow社区e/time in PHP and find out if my timestamp January 11th, 2011 - 6:24 PM is atleast 48 hours old?

Thanks


First, you should be storing the data and time in the appropriate timestamp field and not a text field.

Moving on... You could compare the dates like this:

if(strtotime($dateFromDatabase." + 48 hours") <= strtotime("now")) {
  //It's at least 48 hours old, do your stuff
}


<?php
date_default_timezone_set("America/New_York");
$dtformat = "F jS, Y - g:i A";

$datetime = date($dtformat);

$i_datetime = strtotime($datetime);
$i_48_hours = strtotime('now -48 hours');
if ($i_datetime < $i_48_hours) 
{ 
    echo "The date $datetime is older than 48 hours old.";
}
?>


I would use MySQL DATETIME, but the timezone needs to be different, depending on the users location"

so use mysql timestamp date type, it is designed for such special purposes.

http://dev.mysql.com/doc/refman/5.1/en/timestamp.html

I don't think it is any reason to continue following current terrible solution. Better rewrite your code for using timestamp.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜