php 如何获得自午夜以来经过的秒数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11462894/
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-25 00:34:22  来源:igfitidea点击:

How to get seconds elapsed since midnight

php

提问by Nubcake

Using PHP how do you get the number of seconds elapsed since midnight of the current day?

使用 PHP 如何获得自当天午夜以来经过的秒数?

All i've tried right now is:

我现在所尝试的是:

$hour=substr(date("h:i:s"),0,2);
$minute=substr(date("h:i:s"),3,2);
echo $hour."\r\n";
echo $minute."\r\n";

...but it doesn't return the correct server time of the response and I don't know how to do that.

...但它没有返回响应的正确服务器时间,我不知道该怎么做。

回答by noko

This should work.

这应该有效。

echo time() - strtotime("today");

This will only show your servers timezone though.

不过,这只会显示您的服务器时区。

回答by Wranorn

Simplest I believe would be dividing the current time (in seconds) by the number of seconds in a day (60*60*24) and taking the remainder:

最简单的我相信将当前时间(以秒为单位)除以一天中的秒数(60 * 60 * 24)并取余数:

(time() % 86400)

回答by Tyler Smith

echo (date('G') * 3600 + date('i') * 60);

Multiply the current hour by the number of seconds in each hour and add it to the number of minutes multiplied by the number of seconds in each minute.

将当前小时乘以每小时的秒数,然后将其与分钟数乘以每分钟的秒数相加。

回答by Alex Mantaut

If you are using DateTime:

如果您使用的是日期时间:

 $timeDiff = $time->diff(new \DateTime("today")); 
 $timeDiffSec = $timeDiff->h* 3600 + $timeDiff->i*60 + $timeDiff->s;

回答by Tushar

Based on your comment, if you are receiving the time as a string and want to calculate the number of seconds from that time:

根据您的评论,如果您以字符串形式接收时间并希望计算从该时间开始的秒数:

$time = strtotime($_GET['time']); // Do some verification before this step
$midnight = strtotime("00:00"); // Midnight measured in seconds since Unix Epoch
$sinceMidnight = $time - $midnight; // Seconds since midnight

There you go. Read into time()function of PHP and strtotime().

你去吧。读入PHP 的time()函数和strtotime()

回答by Roman Newaza

echo time() - strtotime('today');

回答by racl101

In Carbon, the number of seconds elapsed since midnight can be found like this:

在 Carbon 中,可以像这样找到自午夜以来经过的秒数:

$seconds_since_midnight = $dt->secondsSinceMidnight();

$seconds_since_midnight = $dt->secondsSinceMidnight();

And though there's no minutes since midnight I suppose you do:

虽然从午夜开始没有几分钟,我想你会这样做:

$minutes_since_midnight = (int) floor($dt->secondsSinceMidnight()/60);

$minutes_since_midnight = (int) floor($dt->secondsSinceMidnight()/60);

回答by Shehzad Nizamani

I think you want to get time from start of the day to current hours and seconds of the day, this can be done like this, you will still need to set your timezone time in place of 'Asia/Karachi'. This gets correct time since midnight in user's timezone instead of server's timezone time.

我认为您想获得从一天开始到当前小时和秒的时间,可以这样做,您仍然需要将时区时间设置为“亚洲/卡拉奇”。这将在用户时区的午夜而不是服务器的时区时间获得正确的时间。

Here is working link: http://codepad.viper-7.com/ykJC2R

这是工作链接:http: //codepad.viper-7.com/ykJC2R

//Get current time timestamp
$time_now = time();

//Create DateTime class object
$date = new DateTime(); 

//Set timestamp to DateTime object
$date->setTimestamp( $time_now );

//Set timezone so that code don't get server's timezone midnight time
$date->setTimezone(new DateTimeZone('Asia/Karachi'));

//Print current time in user's timezone
echo $date->format('Y-m-d H:i') . "<br />";

//Get time stamp for midnight tonight
$date->modify('today midnight');
$midnight_time = $date->getTimestamp();

//Print midnight time in user's timezone
echo $date->format('Y-m-d H:i') . "<br />"; 

    //Now you will need to subtract midnight time from current time in user's timezone
$seconds_since_midnight = $time_now - $midnight_time;

//Print seconds since midnight in your timezone
echo $seconds_since_midnight;

回答by aross

The shortest solution would probably be this:

最短的解决方案可能是这样的:

$time = "12:34:56";
echo strtotime("{$time}UTC", 0);

The reason this works is that strtotimeuses the second parameter to determine the datepart of the time string. In this case, I'm using 0, which means the full string results in 1970-01-01 12:34:56 UTC. strtotimethen returns the UNIX timestamp (the number of seconds elapsed since 1970-01-01 00:00:00 UTC). Note: the first parameter has UTCappended to prevent the time being interpreted as local.

这样做的原因是strtotime使用第二个参数来确定时间字符串的日期部分。在这种情况下,我使用的是0,这意味着完整的字符串结果为1970-01-01 12:34:56 UTC. strtotime然后返回 UNIX 时间戳(自 以来经过的秒数1970-01-01 00:00:00 UTC)。注意:UTC附加了第一个参数以防止时间被解释为本地。

回答by N1ghtwish

I'm keeping the rule that operations on numbers are always faster than operations on strings. When the numbers are integers, operations are even faster (because CPU registers are always "integer wide", means have 32-bit or 64-bit and all other non-floating point types are converted to integer before any operation is performed).

我坚持数字运算总是比字符串运算快的规则。当数字是整数时,运算速度甚至更快(因为 CPU 寄存器总是“整数宽”,意味着有 32 位或 64 位,并且所有其他非浮点类型在执行任何运算之前都会转换为整数)。

Anyway, I found such solution to count seconds till midnight with timezone include:

无论如何,我找到了这样的解决方案来计算秒到午夜的时区包括:

$tillmidnight = 86400 - (time() - mktime(0, 0, 0));

$tillmidnight = 86400 - (time() - mktime(0, 0, 0));

You wanted to get seconds elapsed since midnight so:

您想获得自午夜以来经过的秒数,因此:

$sincemidnight = time() - mktime(0, 0, 0);

$sincemidnight = time() - mktime(0, 0, 0);

It is good to review your php.ini also and set your time zone (date.timezone under [Date] section) there. If you do not have access to php.ini, you can set your time zone in PHP code using date_default_timezone_set(); function.

最好也检查一下您的 php.ini 并在那里设置您的时区([Date] 部分下的 date.timezone)。如果您无权访问 php.ini,您可以使用 date_default_timezone_set() 在 PHP 代码中设置您的时区;功能。