使用PHP在mongodb中插入和检索日期和时间戳
我正在尝试使用mongo作为db,php作为语言来建立一个简单的博客系统。我不知道如何在mongo中输入日期或时间戳(我想我需要一个时间戳,这样我就可以按照发布的降序将帖子拉回来)。我已经在下面发布了我现在作为存根编写的内容——它创建了一个PHP日期并将其插入其中——但这似乎是一个字符串。这是我非常习惯在甲骨文中处理的事情,以至于在mongo中让我大吃一惊。建议使用PHP在mongodb中插入和检索日期和时间戳,php,mongodb,Php,Mongodb,我正在尝试使用mongo作为db,php作为语言来建立一个简单的博客系统。我不知道如何在mongo中输入日期或时间戳(我想我需要一个时间戳,这样我就可以按照发布的降序将帖子拉回来)。我已经在下面发布了我现在作为存根编写的内容——它创建了一个PHP日期并将其插入其中——但这似乎是一个字符串。这是我非常习惯在甲骨文中处理的事情,以至于在mongo中让我大吃一惊。建议 try{ date_default_timezone_set('America/New_York');
try{
date_default_timezone_set('America/New_York');
//$dt = date('j-m-y h-i-s');
$conn = new Mongo(); // connect
$db = $conn->selectDB("blog");
$collection = $db->items;
$item =array(
'title' => $_POST['title'],
'txt' => $_POST['txt'],
'labels' => $_POST['labels'],
'user' => $_POST['user'],
'dt' => date('j-m-y h-i-s')
);
$collection->insert($item);
/// disconnect from server
$conn->close();
} catch ( MongoConnectionException $e ) {
echo '<p>Couldn\'t connect to mongodb, is the "mongo" process running?</p>';
exit();
}
试试看{
日期默认时区设置(“美国/纽约”);
//$dt=日期('j-m-y h-i-s');
$conn=new Mongo();//连接
$db=$conn->selectDB(“博客”);
$collection=$db->items;
$item=数组(
'title'=>$\u POST['title'],
'txt'=>$\u POST['txt'],
“标签”=>$\u贴子[“标签”],
'user'=>$\u POST['user'],
'dt'=>日期('j-m-y h-i-s')
);
$collection->insert($item);
///断开与服务器的连接
$conn->close();
}捕获(MongoConnectionException$e){
echo'无法连接到mongodb,“mongo”进程正在运行吗?;
退出();
}
在我看来,最合适的方法是使用。因此,要插入它,您需要执行以下操作:
$collection->insert(array(
'time' => new MongoDate()
));
这将插入当前日期(或者在新的Mongo2.6中,您可以这样做)
或
将插入一个特定日期
要检索日期,可以使用date('Y-M-dh:i:s',$yourDate->sec)代码>
最新的PHP-MONGO驱动程序更新
使用类型如下:
$collection->insert(array(
'time' => new MongoDB\BSON\UTCDateTime(strtotime("2010-01-15 00:00:00"));
));
您可以使用以下代码在mongo db中插入日期。在mongo db中将当前日期插入为$utcdatetime
/********************建造师**********************************/
$orig_date = new DateTime('2016-06-27 13:03:33');
$orig_date=$orig_date->getTimestamp();
$utcdatetime = new MongoDB\BSON\UTCDateTime($orig_date*1000);
$datetime = $utcdatetime->toDateTime();
$time=$datetime->format(DATE_RSS);
$dateInUTC=$time;
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;
在检索数据时,以毫秒为单位输入变量$utcdatetime并使用以下代码
/********************以UTC为单位检索时间**********************************/
$orig_date = new DateTime('2016-06-27 13:03:33');
$orig_date=$orig_date->getTimestamp();
$utcdatetime = new MongoDB\BSON\UTCDateTime($orig_date*1000);
$datetime = $utcdatetime->toDateTime();
$time=$datetime->format(DATE_RSS);
$dateInUTC=$time;
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;
/********************转换时间本地时区*******************/
$orig_date = new DateTime('2016-06-27 13:03:33');
$orig_date=$orig_date->getTimestamp();
$utcdatetime = new MongoDB\BSON\UTCDateTime($orig_date*1000);
$datetime = $utcdatetime->toDateTime();
$time=$datetime->format(DATE_RSS);
$dateInUTC=$time;
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;
试试这个:MongoDate()对象引用旧的(2016年11月)不受支持的Mongo PHP扩展,仅供将来的读者阅读;新的驱动程序,即MongoDB,还定义了新的类,例如UTCDateTime()@PierpaoloCira。你也可以编辑我的答案或自己写。对不起,我的名声不允许我编辑…:(