将 php 中的时间设置为 00:00:00

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7768716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:21:10  来源:igfitidea点击:

Set time in php to 00:00:00

phptime

提问by Wilest

I need to display the time but it must start from 00:00:00? I've got the following but it uses the current time.

我需要显示时间但它必须从 00:00:00 开始?我有以下内容,但它使用当前时间。

print(date("H:i:s"));

回答by Phil

As an alternative to mktime(), try the newer DateTimeclass, eg

作为 的替代mktime(),尝试更新的DateTime类,例如

$dt = new DateTime;
$dt->setTime(0, 0);
echo $dt->format('H:i:s');

// Add one hour
$dt->add(new DateInterval('PT1H'));
echo $dt->format('H:i:s');

Update

更新

The flexibility of DateIntervalmakes this a very good candidate for a timer, eg

的灵活性DateInterval使其成为计时器的一个很好的候选者,例如

// add 2 years, 1 day and 9 seconds
$dt->add(new DateInterval('P2Y1DT9S'));

回答by Jared Farrish

Use mktime()if you want to start with midnight for the current date:

mktime()如果要从当前日期的午夜开始,请使用:

<?php

echo date('l jS \of F Y h:i:s A',mktime(0,0,0));

?>

OUTPUTS

输出

Friday 14th of October 2011 12:00:00 AM

http://codepad.org/s2NrVfRq

http://codepad.org/s2NrVfRq

In mktime(), you pass arguments for hours, minutes, seconds, month, day, year, so set hours, minutes, secondsto 0to get today at midnight. (Note, as Phil points out, mktime()'s arguments are optional and you can leave month, day, yearout and it will default to the current date).

mktime()你传递参数的hours, minutes, seconds, month, day, year,所以设置hours, minutes, seconds0获得today at midnight。(请注意,正如 Phil 所指出的,mktime()的参数是可选的,您可以month, day, year省略,它将默认为当前日期)。

The mktime()function returns a unix timestamp representing the number of seconds since the unix epoch (January 1, 1970). You can count up from it in seconds or multiples of seconds.

mktime()函数返回一个 unix 时间戳,表示自 unix 纪元(1970 年 1 月 1 日)以来的秒数。您可以以秒或秒的倍数计算。

<?php

// $midnight = mktime(0,0,0,date('m'),date('d'),date('Y'));
// The above is equivalent to below
$midnight = mktime(0,0,0);

echo date('l jS \of F Y h:i:s A',$midnight)."\n";
echo date('l jS \of F Y h:i:s A',$midnight+60)."\n"; // One minute
echo date('l jS \of F Y h:i:s A',$midnight+(60*60))."\n"; // One hour

?>

OUTPUTS

输出

Friday 14th of October 2011 12:00:00 AM
Friday 14th of October 2011 12:01:00 AM
Friday 14th of October 2011 01:00:00 AM

http://codepad.org/FTr98z1n

http://codepad.org/FTr98z1n

回答by ladenedge

date()uses the current time when you don't pass in an explicit timestamp. See the optional argument in the datedocumentation.

date()当您不传入显式时间戳时,使用当前时间。请参阅date文档中的可选参数。

If you want to explicitly format midnight, use:

如果要明确格式化午夜,请使用:

date("H:i:s", mktime(0, 0, 0));

回答by pleerock

try to use this syntax:

尝试使用以下语法:

print(date("H:i:s", 0));

or

或者

print(date("H:i:s", 10)); // 10 seconds