将 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
Set time in php to 00:00:00
提问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 DateTime
class, 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 DateInterval
makes 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
In mktime()
, you pass arguments for hours, minutes, seconds, month, day, year
, so set hours, minutes, seconds
to 0
to get today at midnight
. (Note, as Phil points out, mktime()
's arguments are optional and you can leave month, day, year
out and it will default to the current date).
在mktime()
你传递参数的hours, minutes, seconds, month, day, year
,所以设置hours, minutes, seconds
来0
获得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
回答by ladenedge
date()
uses the current time when you don't pass in an explicit timestamp. See the optional argument in the date
documentation.
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