如何在MongoDB中处理低于1970年的日期 [英] How to handle date lower than 1970 in MongoDB

查看:48
本文介绍了如何在MongoDB中处理低于1970年的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有生日"字段的文档,它的值也可以低于1970年1月1日.我该如何处理? 例如

I have a document with a "birthday" field and this can also have a value lower that 01-01-1970. How can I handle this? E.g.

{....
    'birthday' => newMongoDate(strtotime('31/10/1968')),
 ....
}

这将创建一个生日"值:"1970年1月1日上午12:00:00"(日期时间)

This creates a "birthday" value: "1/1/1970 12:00:00 AM" (DateTime)

推荐答案

您正在运行什么版本的PHP?我在猜PHP<在Windows 5.1上? strtotime()的日期范围应为1901年12月13日格林尼治标准时间(GMT)和2月2038年1月19日星期二格林尼治标准时间(PHP 5.1及更高版本).

What version of PHP are you running? I'm guessing PHP < 5.1 on windows? strtotime() should have a date range of Fri, 13 Dec 1901 20:45:54 GMT and Tue, 19 Jan 2038 03:14:07 GMT with PHP 5.1 and later.

对于PHP 5.2和更高版本,您可以使用:

For PHP 5.2 and greater you could use:

$date = new DateTime('1 Jan 1950');
echo $date->format('Y-m-d');

$date = DateTime::createFromFormat('d M Y','1 Jan 1950');
echo $date->format('Y-m-d');

这篇关于如何在MongoDB中处理低于1970年的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆