在我的代码中,我使用DateTime对象来操作日期,然后将它们转换为时间戳,以便将它们保存在一些JSON文件中.
出于某些原因,我希望与DateTime(或接近的东西)具有相同的功能,但具有微秒精度(我在插入JSON文件时将转换为float).
我的问题是:是否有一个PHP对象就是喜欢DateTime,但能处理微秒吗?
目标是能够使用对象操纵microtimes.
在date()文档中,有一些东西表明可以用微秒创建DateTime,但我无法找到如何.
u微秒(在PHP 5.2.2中添加).请注意,date()将始终生成000000,因为它采用整数参数,而DateTime :: format()确实支持微秒,如果使用微秒创建DateTime.
我曾尝试使用浮点值(microtime(true))设置DateTime对象的时间戳,但它不起作用(我认为它将时间戳转换为int,导致丢失微秒).
这是我试过的方式
$dt = new DateTime();
$dt->setTimestamp(3.4); // I replaced 3.4 by microtime(true), this is just to give an example
var_dump($dt);
var_dump($dt->format('u'));
Run Code Online (Sandbox Code Playgroud)
这里.4没有考虑到这一点(即使我们可以使用u与微秒相对应的格式).
object(DateTime)[1]
public 'date' => string '1970-01-01 01:00:03' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
string '000000' (length=6)
Run Code Online (Sandbox Code Playgroud)
编辑:我看到这个代码,它允许在DateTime中添加微秒,但我需要在创建DateTime之前对microtime应用大量修改.由于我将使用它很多,我想在获得"microtime对象"之前尽可能少地修改microtime.
$d = new DateTime("15-07-2014 18:30:00.111111");
Run Code Online (Sandbox Code Playgroud) 为了包含正确的文件并在发生错误时显示错误页面,我有以下代码(非常简化):
$page = 'examplePage.php';
$page404 = '404.php';
if (file_exists($page))
{
require($page);
}
else if (file_exists($page404))
{
require($page404);
}
else
{
// Tell the browser to display his default page
}
?>
Run Code Online (Sandbox Code Playgroud)
总结一下:
如果我有该文件,我会将其包含在内。
如果我没有该文件,我将包含错误文件。
如果错误文件也不存在怎么办?
我希望它被呈现为浏览器的默认错误页面。
我已经使用 Internet Explorer 通过发送带有 HTTP 错误的空内容来实现这一点。问题是其他浏览器的行为不一样,它们都显示一个空白页面。
有没有办法告诉浏览器显示自己的错误页面?(不仅是 404,还有所有错误:304、500 等)
谢谢你。
编辑:我忘了告诉你,我可以完全控制我发送的标题和响应发送的内容。
编辑2:这是一些代码
// possible paths to retrieve the file
$possiblePaths = array(
$urlPath,
D_ROOT.$urlPath,
D_PAGES.$urlPath.'.php',
D_PAGES.$urlPath.'/index.php',
$urlPath.'.php'
);
foreach ($possiblePaths as $possiblePath)
if (file_exists($possiblePath) && !is_dir($possiblePath))
{ …Run Code Online (Sandbox Code Playgroud)